fsp_srv.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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/hex_util.h"
  13. #include "common/logging/log.h"
  14. #include "common/string_util.h"
  15. #include "core/core.h"
  16. #include "core/file_sys/directory.h"
  17. #include "core/file_sys/errors.h"
  18. #include "core/file_sys/mode.h"
  19. #include "core/file_sys/nca_metadata.h"
  20. #include "core/file_sys/patch_manager.h"
  21. #include "core/file_sys/romfs_factory.h"
  22. #include "core/file_sys/savedata_factory.h"
  23. #include "core/file_sys/system_archive/system_archive.h"
  24. #include "core/file_sys/vfs.h"
  25. #include "core/hle/ipc_helpers.h"
  26. #include "core/hle/kernel/k_process.h"
  27. #include "core/hle/service/filesystem/filesystem.h"
  28. #include "core/hle/service/filesystem/fsp_srv.h"
  29. #include "core/reporter.h"
  30. namespace Service::FileSystem {
  31. struct SizeGetter {
  32. std::function<u64()> get_free_size;
  33. std::function<u64()> get_total_size;
  34. static SizeGetter FromStorageId(const FileSystemController& fsc, FileSys::StorageId id) {
  35. return {
  36. [&fsc, id] { return fsc.GetFreeSpaceSize(id); },
  37. [&fsc, id] { return fsc.GetTotalSpaceSize(id); },
  38. };
  39. }
  40. };
  41. enum class FileSystemType : u8 {
  42. Invalid0 = 0,
  43. Invalid1 = 1,
  44. Logo = 2,
  45. ContentControl = 3,
  46. ContentManual = 4,
  47. ContentMeta = 5,
  48. ContentData = 6,
  49. ApplicationPackage = 7,
  50. };
  51. class IStorage final : public ServiceFramework<IStorage> {
  52. public:
  53. explicit IStorage(Core::System& system_, FileSys::VirtualFile backend_)
  54. : ServiceFramework{system_, "IStorage"}, backend(std::move(backend_)) {
  55. static const FunctionInfo functions[] = {
  56. {0, &IStorage::Read, "Read"},
  57. {1, nullptr, "Write"},
  58. {2, nullptr, "Flush"},
  59. {3, nullptr, "SetSize"},
  60. {4, &IStorage::GetSize, "GetSize"},
  61. {5, nullptr, "OperateRange"},
  62. };
  63. RegisterHandlers(functions);
  64. }
  65. private:
  66. FileSys::VirtualFile backend;
  67. void Read(Kernel::HLERequestContext& ctx) {
  68. IPC::RequestParser rp{ctx};
  69. const s64 offset = rp.Pop<s64>();
  70. const s64 length = rp.Pop<s64>();
  71. LOG_DEBUG(Service_FS, "called, offset=0x{:X}, length={}", offset, length);
  72. // Error checking
  73. if (length < 0) {
  74. LOG_ERROR(Service_FS, "Length is less than 0, length={}", length);
  75. IPC::ResponseBuilder rb{ctx, 2};
  76. rb.Push(FileSys::ERROR_INVALID_SIZE);
  77. return;
  78. }
  79. if (offset < 0) {
  80. LOG_ERROR(Service_FS, "Offset is less than 0, offset={}", offset);
  81. IPC::ResponseBuilder rb{ctx, 2};
  82. rb.Push(FileSys::ERROR_INVALID_OFFSET);
  83. return;
  84. }
  85. // Read the data from the Storage backend
  86. std::vector<u8> output = backend->ReadBytes(length, offset);
  87. // Write the data to memory
  88. ctx.WriteBuffer(output);
  89. IPC::ResponseBuilder rb{ctx, 2};
  90. rb.Push(RESULT_SUCCESS);
  91. }
  92. void GetSize(Kernel::HLERequestContext& ctx) {
  93. const u64 size = backend->GetSize();
  94. LOG_DEBUG(Service_FS, "called, size={}", size);
  95. IPC::ResponseBuilder rb{ctx, 4};
  96. rb.Push(RESULT_SUCCESS);
  97. rb.Push<u64>(size);
  98. }
  99. };
  100. class IFile final : public ServiceFramework<IFile> {
  101. public:
  102. explicit IFile(Core::System& system_, FileSys::VirtualFile backend_)
  103. : ServiceFramework{system_, "IFile"}, backend(std::move(backend_)) {
  104. static const FunctionInfo functions[] = {
  105. {0, &IFile::Read, "Read"},
  106. {1, &IFile::Write, "Write"},
  107. {2, &IFile::Flush, "Flush"},
  108. {3, &IFile::SetSize, "SetSize"},
  109. {4, &IFile::GetSize, "GetSize"},
  110. {5, nullptr, "OperateRange"},
  111. {6, nullptr, "OperateRangeWithBuffer"},
  112. };
  113. RegisterHandlers(functions);
  114. }
  115. private:
  116. FileSys::VirtualFile backend;
  117. void Read(Kernel::HLERequestContext& ctx) {
  118. IPC::RequestParser rp{ctx};
  119. const u64 option = rp.Pop<u64>();
  120. const s64 offset = rp.Pop<s64>();
  121. const s64 length = rp.Pop<s64>();
  122. LOG_DEBUG(Service_FS, "called, option={}, offset=0x{:X}, length={}", option, offset,
  123. length);
  124. // Error checking
  125. if (length < 0) {
  126. LOG_ERROR(Service_FS, "Length is less than 0, length={}", length);
  127. IPC::ResponseBuilder rb{ctx, 2};
  128. rb.Push(FileSys::ERROR_INVALID_SIZE);
  129. return;
  130. }
  131. if (offset < 0) {
  132. LOG_ERROR(Service_FS, "Offset is less than 0, offset={}", offset);
  133. IPC::ResponseBuilder rb{ctx, 2};
  134. rb.Push(FileSys::ERROR_INVALID_OFFSET);
  135. return;
  136. }
  137. // Read the data from the Storage backend
  138. std::vector<u8> output = backend->ReadBytes(length, offset);
  139. // Write the data to memory
  140. ctx.WriteBuffer(output);
  141. IPC::ResponseBuilder rb{ctx, 4};
  142. rb.Push(RESULT_SUCCESS);
  143. rb.Push(static_cast<u64>(output.size()));
  144. }
  145. void Write(Kernel::HLERequestContext& ctx) {
  146. IPC::RequestParser rp{ctx};
  147. const u64 option = rp.Pop<u64>();
  148. const s64 offset = rp.Pop<s64>();
  149. const s64 length = rp.Pop<s64>();
  150. LOG_DEBUG(Service_FS, "called, option={}, offset=0x{:X}, length={}", option, offset,
  151. length);
  152. // Error checking
  153. if (length < 0) {
  154. LOG_ERROR(Service_FS, "Length is less than 0, length={}", length);
  155. IPC::ResponseBuilder rb{ctx, 2};
  156. rb.Push(FileSys::ERROR_INVALID_SIZE);
  157. return;
  158. }
  159. if (offset < 0) {
  160. LOG_ERROR(Service_FS, "Offset is less than 0, offset={}", offset);
  161. IPC::ResponseBuilder rb{ctx, 2};
  162. rb.Push(FileSys::ERROR_INVALID_OFFSET);
  163. return;
  164. }
  165. const std::vector<u8> data = ctx.ReadBuffer();
  166. ASSERT_MSG(
  167. static_cast<s64>(data.size()) <= length,
  168. "Attempting to write more data than requested (requested={:016X}, actual={:016X}).",
  169. length, data.size());
  170. // Write the data to the Storage backend
  171. const auto write_size =
  172. static_cast<std::size_t>(std::distance(data.begin(), data.begin() + length));
  173. const std::size_t written = backend->Write(data.data(), write_size, offset);
  174. ASSERT_MSG(static_cast<s64>(written) == length,
  175. "Could not write all bytes to file (requested={:016X}, actual={:016X}).", length,
  176. written);
  177. IPC::ResponseBuilder rb{ctx, 2};
  178. rb.Push(RESULT_SUCCESS);
  179. }
  180. void Flush(Kernel::HLERequestContext& ctx) {
  181. LOG_DEBUG(Service_FS, "called");
  182. // Exists for SDK compatibiltity -- No need to flush file.
  183. IPC::ResponseBuilder rb{ctx, 2};
  184. rb.Push(RESULT_SUCCESS);
  185. }
  186. void SetSize(Kernel::HLERequestContext& ctx) {
  187. IPC::RequestParser rp{ctx};
  188. const u64 size = rp.Pop<u64>();
  189. LOG_DEBUG(Service_FS, "called, size={}", size);
  190. backend->Resize(size);
  191. IPC::ResponseBuilder rb{ctx, 2};
  192. rb.Push(RESULT_SUCCESS);
  193. }
  194. void GetSize(Kernel::HLERequestContext& ctx) {
  195. const u64 size = backend->GetSize();
  196. LOG_DEBUG(Service_FS, "called, size={}", size);
  197. IPC::ResponseBuilder rb{ctx, 4};
  198. rb.Push(RESULT_SUCCESS);
  199. rb.Push<u64>(size);
  200. }
  201. };
  202. template <typename T>
  203. static void BuildEntryIndex(std::vector<FileSys::Entry>& entries, const std::vector<T>& new_data,
  204. FileSys::EntryType type) {
  205. entries.reserve(entries.size() + new_data.size());
  206. for (const auto& new_entry : new_data) {
  207. entries.emplace_back(new_entry->GetName(), type, new_entry->GetSize());
  208. }
  209. }
  210. class IDirectory final : public ServiceFramework<IDirectory> {
  211. public:
  212. explicit IDirectory(Core::System& system_, FileSys::VirtualDir backend_)
  213. : ServiceFramework{system_, "IDirectory"}, backend(std::move(backend_)) {
  214. static const FunctionInfo functions[] = {
  215. {0, &IDirectory::Read, "Read"},
  216. {1, &IDirectory::GetEntryCount, "GetEntryCount"},
  217. };
  218. RegisterHandlers(functions);
  219. // TODO(DarkLordZach): Verify that this is the correct behavior.
  220. // Build entry index now to save time later.
  221. BuildEntryIndex(entries, backend->GetFiles(), FileSys::EntryType::File);
  222. BuildEntryIndex(entries, backend->GetSubdirectories(), FileSys::EntryType::Directory);
  223. }
  224. private:
  225. FileSys::VirtualDir backend;
  226. std::vector<FileSys::Entry> entries;
  227. u64 next_entry_index = 0;
  228. void Read(Kernel::HLERequestContext& ctx) {
  229. LOG_DEBUG(Service_FS, "called.");
  230. // Calculate how many entries we can fit in the output buffer
  231. const u64 count_entries = ctx.GetWriteBufferSize() / sizeof(FileSys::Entry);
  232. // Cap at total number of entries.
  233. const u64 actual_entries = std::min(count_entries, entries.size() - next_entry_index);
  234. // Determine data start and end
  235. const auto* begin = reinterpret_cast<u8*>(entries.data() + next_entry_index);
  236. const auto* end = reinterpret_cast<u8*>(entries.data() + next_entry_index + actual_entries);
  237. const auto range_size = static_cast<std::size_t>(std::distance(begin, end));
  238. next_entry_index += actual_entries;
  239. // Write the data to memory
  240. ctx.WriteBuffer(begin, range_size);
  241. IPC::ResponseBuilder rb{ctx, 4};
  242. rb.Push(RESULT_SUCCESS);
  243. rb.Push(actual_entries);
  244. }
  245. void GetEntryCount(Kernel::HLERequestContext& ctx) {
  246. LOG_DEBUG(Service_FS, "called");
  247. u64 count = entries.size() - next_entry_index;
  248. IPC::ResponseBuilder rb{ctx, 4};
  249. rb.Push(RESULT_SUCCESS);
  250. rb.Push(count);
  251. }
  252. };
  253. class IFileSystem final : public ServiceFramework<IFileSystem> {
  254. public:
  255. explicit IFileSystem(Core::System& system_, FileSys::VirtualDir backend_, SizeGetter size_)
  256. : ServiceFramework{system_, "IFileSystem"}, backend{std::move(backend_)}, size{std::move(
  257. size_)} {
  258. static const FunctionInfo functions[] = {
  259. {0, &IFileSystem::CreateFile, "CreateFile"},
  260. {1, &IFileSystem::DeleteFile, "DeleteFile"},
  261. {2, &IFileSystem::CreateDirectory, "CreateDirectory"},
  262. {3, &IFileSystem::DeleteDirectory, "DeleteDirectory"},
  263. {4, &IFileSystem::DeleteDirectoryRecursively, "DeleteDirectoryRecursively"},
  264. {5, &IFileSystem::RenameFile, "RenameFile"},
  265. {6, nullptr, "RenameDirectory"},
  266. {7, &IFileSystem::GetEntryType, "GetEntryType"},
  267. {8, &IFileSystem::OpenFile, "OpenFile"},
  268. {9, &IFileSystem::OpenDirectory, "OpenDirectory"},
  269. {10, &IFileSystem::Commit, "Commit"},
  270. {11, &IFileSystem::GetFreeSpaceSize, "GetFreeSpaceSize"},
  271. {12, &IFileSystem::GetTotalSpaceSize, "GetTotalSpaceSize"},
  272. {13, &IFileSystem::CleanDirectoryRecursively, "CleanDirectoryRecursively"},
  273. {14, nullptr, "GetFileTimeStampRaw"},
  274. {15, nullptr, "QueryEntry"},
  275. };
  276. RegisterHandlers(functions);
  277. }
  278. void CreateFile(Kernel::HLERequestContext& ctx) {
  279. IPC::RequestParser rp{ctx};
  280. const auto file_buffer = ctx.ReadBuffer();
  281. const std::string name = Common::StringFromBuffer(file_buffer);
  282. const u64 file_mode = rp.Pop<u64>();
  283. const u32 file_size = rp.Pop<u32>();
  284. LOG_DEBUG(Service_FS, "called. file={}, mode=0x{:X}, size=0x{:08X}", name, file_mode,
  285. file_size);
  286. IPC::ResponseBuilder rb{ctx, 2};
  287. rb.Push(backend.CreateFile(name, file_size));
  288. }
  289. void DeleteFile(Kernel::HLERequestContext& ctx) {
  290. const auto file_buffer = ctx.ReadBuffer();
  291. const std::string name = Common::StringFromBuffer(file_buffer);
  292. LOG_DEBUG(Service_FS, "called. file={}", name);
  293. IPC::ResponseBuilder rb{ctx, 2};
  294. rb.Push(backend.DeleteFile(name));
  295. }
  296. void CreateDirectory(Kernel::HLERequestContext& ctx) {
  297. const auto file_buffer = ctx.ReadBuffer();
  298. const std::string name = Common::StringFromBuffer(file_buffer);
  299. LOG_DEBUG(Service_FS, "called. directory={}", name);
  300. IPC::ResponseBuilder rb{ctx, 2};
  301. rb.Push(backend.CreateDirectory(name));
  302. }
  303. void DeleteDirectory(Kernel::HLERequestContext& ctx) {
  304. const auto file_buffer = ctx.ReadBuffer();
  305. const std::string name = Common::StringFromBuffer(file_buffer);
  306. LOG_DEBUG(Service_FS, "called. directory={}", name);
  307. IPC::ResponseBuilder rb{ctx, 2};
  308. rb.Push(backend.DeleteDirectory(name));
  309. }
  310. void DeleteDirectoryRecursively(Kernel::HLERequestContext& ctx) {
  311. const auto file_buffer = ctx.ReadBuffer();
  312. const std::string name = Common::StringFromBuffer(file_buffer);
  313. LOG_DEBUG(Service_FS, "called. directory={}", name);
  314. IPC::ResponseBuilder rb{ctx, 2};
  315. rb.Push(backend.DeleteDirectoryRecursively(name));
  316. }
  317. void CleanDirectoryRecursively(Kernel::HLERequestContext& ctx) {
  318. const auto file_buffer = ctx.ReadBuffer();
  319. const std::string name = Common::StringFromBuffer(file_buffer);
  320. LOG_DEBUG(Service_FS, "called. Directory: {}", name);
  321. IPC::ResponseBuilder rb{ctx, 2};
  322. rb.Push(backend.CleanDirectoryRecursively(name));
  323. }
  324. void RenameFile(Kernel::HLERequestContext& ctx) {
  325. std::vector<u8> buffer = ctx.ReadBuffer(0);
  326. const std::string src_name = Common::StringFromBuffer(buffer);
  327. buffer = ctx.ReadBuffer(1);
  328. const std::string dst_name = Common::StringFromBuffer(buffer);
  329. LOG_DEBUG(Service_FS, "called. file '{}' to file '{}'", src_name, dst_name);
  330. IPC::ResponseBuilder rb{ctx, 2};
  331. rb.Push(backend.RenameFile(src_name, dst_name));
  332. }
  333. void OpenFile(Kernel::HLERequestContext& ctx) {
  334. IPC::RequestParser rp{ctx};
  335. const auto file_buffer = ctx.ReadBuffer();
  336. const std::string name = Common::StringFromBuffer(file_buffer);
  337. const auto mode = static_cast<FileSys::Mode>(rp.Pop<u32>());
  338. LOG_DEBUG(Service_FS, "called. file={}, mode={}", name, mode);
  339. auto result = backend.OpenFile(name, mode);
  340. if (result.Failed()) {
  341. IPC::ResponseBuilder rb{ctx, 2};
  342. rb.Push(result.Code());
  343. return;
  344. }
  345. auto file = std::make_shared<IFile>(system, result.Unwrap());
  346. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  347. rb.Push(RESULT_SUCCESS);
  348. rb.PushIpcInterface<IFile>(std::move(file));
  349. }
  350. void OpenDirectory(Kernel::HLERequestContext& ctx) {
  351. IPC::RequestParser rp{ctx};
  352. const auto file_buffer = ctx.ReadBuffer();
  353. const std::string name = Common::StringFromBuffer(file_buffer);
  354. // TODO(Subv): Implement this filter.
  355. const u32 filter_flags = rp.Pop<u32>();
  356. LOG_DEBUG(Service_FS, "called. directory={}, filter={}", name, filter_flags);
  357. auto result = backend.OpenDirectory(name);
  358. if (result.Failed()) {
  359. IPC::ResponseBuilder rb{ctx, 2};
  360. rb.Push(result.Code());
  361. return;
  362. }
  363. auto directory = std::make_shared<IDirectory>(system, result.Unwrap());
  364. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  365. rb.Push(RESULT_SUCCESS);
  366. rb.PushIpcInterface<IDirectory>(std::move(directory));
  367. }
  368. void GetEntryType(Kernel::HLERequestContext& ctx) {
  369. const auto file_buffer = ctx.ReadBuffer();
  370. const std::string name = Common::StringFromBuffer(file_buffer);
  371. LOG_DEBUG(Service_FS, "called. file={}", name);
  372. auto result = backend.GetEntryType(name);
  373. if (result.Failed()) {
  374. IPC::ResponseBuilder rb{ctx, 2};
  375. rb.Push(result.Code());
  376. return;
  377. }
  378. IPC::ResponseBuilder rb{ctx, 3};
  379. rb.Push(RESULT_SUCCESS);
  380. rb.Push<u32>(static_cast<u32>(*result));
  381. }
  382. void Commit(Kernel::HLERequestContext& ctx) {
  383. LOG_WARNING(Service_FS, "(STUBBED) called");
  384. IPC::ResponseBuilder rb{ctx, 2};
  385. rb.Push(RESULT_SUCCESS);
  386. }
  387. void GetFreeSpaceSize(Kernel::HLERequestContext& ctx) {
  388. LOG_DEBUG(Service_FS, "called");
  389. IPC::ResponseBuilder rb{ctx, 4};
  390. rb.Push(RESULT_SUCCESS);
  391. rb.Push(size.get_free_size());
  392. }
  393. void GetTotalSpaceSize(Kernel::HLERequestContext& ctx) {
  394. LOG_DEBUG(Service_FS, "called");
  395. IPC::ResponseBuilder rb{ctx, 4};
  396. rb.Push(RESULT_SUCCESS);
  397. rb.Push(size.get_total_size());
  398. }
  399. private:
  400. VfsDirectoryServiceWrapper backend;
  401. SizeGetter size;
  402. };
  403. class ISaveDataInfoReader final : public ServiceFramework<ISaveDataInfoReader> {
  404. public:
  405. explicit ISaveDataInfoReader(Core::System& system_, FileSys::SaveDataSpaceId space,
  406. FileSystemController& fsc_)
  407. : ServiceFramework{system_, "ISaveDataInfoReader"}, fsc{fsc_} {
  408. static const FunctionInfo functions[] = {
  409. {0, &ISaveDataInfoReader::ReadSaveDataInfo, "ReadSaveDataInfo"},
  410. };
  411. RegisterHandlers(functions);
  412. FindAllSaves(space);
  413. }
  414. void ReadSaveDataInfo(Kernel::HLERequestContext& ctx) {
  415. LOG_DEBUG(Service_FS, "called");
  416. // Calculate how many entries we can fit in the output buffer
  417. const u64 count_entries = ctx.GetWriteBufferSize() / sizeof(SaveDataInfo);
  418. // Cap at total number of entries.
  419. const u64 actual_entries = std::min(count_entries, info.size() - next_entry_index);
  420. // Determine data start and end
  421. const auto* begin = reinterpret_cast<u8*>(info.data() + next_entry_index);
  422. const auto* end = reinterpret_cast<u8*>(info.data() + next_entry_index + actual_entries);
  423. const auto range_size = static_cast<std::size_t>(std::distance(begin, end));
  424. next_entry_index += actual_entries;
  425. // Write the data to memory
  426. ctx.WriteBuffer(begin, range_size);
  427. IPC::ResponseBuilder rb{ctx, 3};
  428. rb.Push(RESULT_SUCCESS);
  429. rb.Push<u32>(static_cast<u32>(actual_entries));
  430. }
  431. private:
  432. static u64 stoull_be(std::string_view str) {
  433. if (str.size() != 16)
  434. return 0;
  435. const auto bytes = Common::HexStringToArray<0x8>(str);
  436. u64 out{};
  437. std::memcpy(&out, bytes.data(), sizeof(u64));
  438. return Common::swap64(out);
  439. }
  440. void FindAllSaves(FileSys::SaveDataSpaceId space) {
  441. const auto save_root = fsc.OpenSaveDataSpace(space);
  442. if (save_root.Failed() || *save_root == nullptr) {
  443. LOG_ERROR(Service_FS, "The save root for the space_id={:02X} was invalid!", space);
  444. return;
  445. }
  446. for (const auto& type : (*save_root)->GetSubdirectories()) {
  447. if (type->GetName() == "save") {
  448. for (const auto& save_id : type->GetSubdirectories()) {
  449. for (const auto& user_id : save_id->GetSubdirectories()) {
  450. const auto save_id_numeric = stoull_be(save_id->GetName());
  451. auto user_id_numeric = Common::HexStringToArray<0x10>(user_id->GetName());
  452. std::reverse(user_id_numeric.begin(), user_id_numeric.end());
  453. if (save_id_numeric != 0) {
  454. // System Save Data
  455. info.emplace_back(SaveDataInfo{
  456. 0,
  457. space,
  458. FileSys::SaveDataType::SystemSaveData,
  459. {},
  460. user_id_numeric,
  461. save_id_numeric,
  462. 0,
  463. user_id->GetSize(),
  464. {},
  465. {},
  466. });
  467. continue;
  468. }
  469. for (const auto& title_id : user_id->GetSubdirectories()) {
  470. const auto device =
  471. std::all_of(user_id_numeric.begin(), user_id_numeric.end(),
  472. [](u8 val) { return val == 0; });
  473. info.emplace_back(SaveDataInfo{
  474. 0,
  475. space,
  476. device ? FileSys::SaveDataType::DeviceSaveData
  477. : FileSys::SaveDataType::SaveData,
  478. {},
  479. user_id_numeric,
  480. save_id_numeric,
  481. stoull_be(title_id->GetName()),
  482. title_id->GetSize(),
  483. {},
  484. {},
  485. });
  486. }
  487. }
  488. }
  489. } else if (space == FileSys::SaveDataSpaceId::TemporaryStorage) {
  490. // Temporary Storage
  491. for (const auto& user_id : type->GetSubdirectories()) {
  492. for (const auto& title_id : user_id->GetSubdirectories()) {
  493. if (!title_id->GetFiles().empty() ||
  494. !title_id->GetSubdirectories().empty()) {
  495. auto user_id_numeric =
  496. Common::HexStringToArray<0x10>(user_id->GetName());
  497. std::reverse(user_id_numeric.begin(), user_id_numeric.end());
  498. info.emplace_back(SaveDataInfo{
  499. 0,
  500. space,
  501. FileSys::SaveDataType::TemporaryStorage,
  502. {},
  503. user_id_numeric,
  504. stoull_be(type->GetName()),
  505. stoull_be(title_id->GetName()),
  506. title_id->GetSize(),
  507. {},
  508. {},
  509. });
  510. }
  511. }
  512. }
  513. }
  514. }
  515. }
  516. struct SaveDataInfo {
  517. u64_le save_id_unknown;
  518. FileSys::SaveDataSpaceId space;
  519. FileSys::SaveDataType type;
  520. INSERT_PADDING_BYTES(0x6);
  521. std::array<u8, 0x10> user_id;
  522. u64_le save_id;
  523. u64_le title_id;
  524. u64_le save_image_size;
  525. u16_le index;
  526. FileSys::SaveDataRank rank;
  527. INSERT_PADDING_BYTES(0x25);
  528. };
  529. static_assert(sizeof(SaveDataInfo) == 0x60, "SaveDataInfo has incorrect size.");
  530. FileSystemController& fsc;
  531. std::vector<SaveDataInfo> info;
  532. u64 next_entry_index = 0;
  533. };
  534. FSP_SRV::FSP_SRV(Core::System& system_)
  535. : ServiceFramework{system_, "fsp-srv"}, fsc{system.GetFileSystemController()},
  536. content_provider{system.GetContentProvider()}, reporter{system.GetReporter()} {
  537. // clang-format off
  538. static const FunctionInfo functions[] = {
  539. {0, nullptr, "OpenFileSystem"},
  540. {1, &FSP_SRV::SetCurrentProcess, "SetCurrentProcess"},
  541. {2, nullptr, "OpenDataFileSystemByCurrentProcess"},
  542. {7, &FSP_SRV::OpenFileSystemWithPatch, "OpenFileSystemWithPatch"},
  543. {8, nullptr, "OpenFileSystemWithId"},
  544. {9, nullptr, "OpenDataFileSystemByApplicationId"},
  545. {11, nullptr, "OpenBisFileSystem"},
  546. {12, nullptr, "OpenBisStorage"},
  547. {13, nullptr, "InvalidateBisCache"},
  548. {17, nullptr, "OpenHostFileSystem"},
  549. {18, &FSP_SRV::OpenSdCardFileSystem, "OpenSdCardFileSystem"},
  550. {19, nullptr, "FormatSdCardFileSystem"},
  551. {21, nullptr, "DeleteSaveDataFileSystem"},
  552. {22, &FSP_SRV::CreateSaveDataFileSystem, "CreateSaveDataFileSystem"},
  553. {23, nullptr, "CreateSaveDataFileSystemBySystemSaveDataId"},
  554. {24, nullptr, "RegisterSaveDataFileSystemAtomicDeletion"},
  555. {25, nullptr, "DeleteSaveDataFileSystemBySaveDataSpaceId"},
  556. {26, nullptr, "FormatSdCardDryRun"},
  557. {27, nullptr, "IsExFatSupported"},
  558. {28, nullptr, "DeleteSaveDataFileSystemBySaveDataAttribute"},
  559. {30, nullptr, "OpenGameCardStorage"},
  560. {31, nullptr, "OpenGameCardFileSystem"},
  561. {32, nullptr, "ExtendSaveDataFileSystem"},
  562. {33, nullptr, "DeleteCacheStorage"},
  563. {34, nullptr, "GetCacheStorageSize"},
  564. {35, nullptr, "CreateSaveDataFileSystemByHashSalt"},
  565. {36, nullptr, "OpenHostFileSystemWithOption"},
  566. {51, &FSP_SRV::OpenSaveDataFileSystem, "OpenSaveDataFileSystem"},
  567. {52, nullptr, "OpenSaveDataFileSystemBySystemSaveDataId"},
  568. {53, &FSP_SRV::OpenReadOnlySaveDataFileSystem, "OpenReadOnlySaveDataFileSystem"},
  569. {57, nullptr, "ReadSaveDataFileSystemExtraDataBySaveDataSpaceId"},
  570. {58, nullptr, "ReadSaveDataFileSystemExtraData"},
  571. {59, nullptr, "WriteSaveDataFileSystemExtraData"},
  572. {60, nullptr, "OpenSaveDataInfoReader"},
  573. {61, &FSP_SRV::OpenSaveDataInfoReaderBySaveDataSpaceId, "OpenSaveDataInfoReaderBySaveDataSpaceId"},
  574. {62, nullptr, "OpenCacheStorageList"},
  575. {64, nullptr, "OpenSaveDataInternalStorageFileSystem"},
  576. {65, nullptr, "UpdateSaveDataMacForDebug"},
  577. {66, nullptr, "WriteSaveDataFileSystemExtraData2"},
  578. {67, nullptr, "FindSaveDataWithFilter"},
  579. {68, nullptr, "OpenSaveDataInfoReaderBySaveDataFilter"},
  580. {69, nullptr, "ReadSaveDataFileSystemExtraDataBySaveDataAttribute"},
  581. {70, &FSP_SRV::WriteSaveDataFileSystemExtraDataBySaveDataAttribute, "WriteSaveDataFileSystemExtraDataBySaveDataAttribute"},
  582. {71, &FSP_SRV::ReadSaveDataFileSystemExtraDataWithMaskBySaveDataAttribute, "ReadSaveDataFileSystemExtraDataWithMaskBySaveDataAttribute"},
  583. {80, nullptr, "OpenSaveDataMetaFile"},
  584. {81, nullptr, "OpenSaveDataTransferManager"},
  585. {82, nullptr, "OpenSaveDataTransferManagerVersion2"},
  586. {83, nullptr, "OpenSaveDataTransferProhibiterForCloudBackUp"},
  587. {84, nullptr, "ListApplicationAccessibleSaveDataOwnerId"},
  588. {85, nullptr, "OpenSaveDataTransferManagerForSaveDataRepair"},
  589. {86, nullptr, "OpenSaveDataMover"},
  590. {87, nullptr, "OpenSaveDataTransferManagerForRepair"},
  591. {100, nullptr, "OpenImageDirectoryFileSystem"},
  592. {101, nullptr, "OpenBaseFileSystem"},
  593. {102, nullptr, "FormatBaseFileSystem"},
  594. {110, nullptr, "OpenContentStorageFileSystem"},
  595. {120, nullptr, "OpenCloudBackupWorkStorageFileSystem"},
  596. {130, nullptr, "OpenCustomStorageFileSystem"},
  597. {200, &FSP_SRV::OpenDataStorageByCurrentProcess, "OpenDataStorageByCurrentProcess"},
  598. {201, nullptr, "OpenDataStorageByProgramId"},
  599. {202, &FSP_SRV::OpenDataStorageByDataId, "OpenDataStorageByDataId"},
  600. {203, &FSP_SRV::OpenPatchDataStorageByCurrentProcess, "OpenPatchDataStorageByCurrentProcess"},
  601. {204, nullptr, "OpenDataFileSystemByProgramIndex"},
  602. {205, &FSP_SRV::OpenDataStorageWithProgramIndex, "OpenDataStorageWithProgramIndex"},
  603. {400, nullptr, "OpenDeviceOperator"},
  604. {500, nullptr, "OpenSdCardDetectionEventNotifier"},
  605. {501, nullptr, "OpenGameCardDetectionEventNotifier"},
  606. {510, nullptr, "OpenSystemDataUpdateEventNotifier"},
  607. {511, nullptr, "NotifySystemDataUpdateEvent"},
  608. {520, nullptr, "SimulateGameCardDetectionEvent"},
  609. {600, nullptr, "SetCurrentPosixTime"},
  610. {601, nullptr, "QuerySaveDataTotalSize"},
  611. {602, nullptr, "VerifySaveDataFileSystem"},
  612. {603, nullptr, "CorruptSaveDataFileSystem"},
  613. {604, nullptr, "CreatePaddingFile"},
  614. {605, nullptr, "DeleteAllPaddingFiles"},
  615. {606, nullptr, "GetRightsId"},
  616. {607, nullptr, "RegisterExternalKey"},
  617. {608, nullptr, "UnregisterAllExternalKey"},
  618. {609, nullptr, "GetRightsIdByPath"},
  619. {610, nullptr, "GetRightsIdAndKeyGenerationByPath"},
  620. {611, nullptr, "SetCurrentPosixTimeWithTimeDifference"},
  621. {612, nullptr, "GetFreeSpaceSizeForSaveData"},
  622. {613, nullptr, "VerifySaveDataFileSystemBySaveDataSpaceId"},
  623. {614, nullptr, "CorruptSaveDataFileSystemBySaveDataSpaceId"},
  624. {615, nullptr, "QuerySaveDataInternalStorageTotalSize"},
  625. {616, nullptr, "GetSaveDataCommitId"},
  626. {617, nullptr, "UnregisterExternalKey"},
  627. {620, nullptr, "SetSdCardEncryptionSeed"},
  628. {630, nullptr, "SetSdCardAccessibility"},
  629. {631, nullptr, "IsSdCardAccessible"},
  630. {640, nullptr, "IsSignedSystemPartitionOnSdCardValid"},
  631. {700, nullptr, "OpenAccessFailureResolver"},
  632. {701, nullptr, "GetAccessFailureDetectionEvent"},
  633. {702, nullptr, "IsAccessFailureDetected"},
  634. {710, nullptr, "ResolveAccessFailure"},
  635. {720, nullptr, "AbandonAccessFailure"},
  636. {800, nullptr, "GetAndClearFileSystemProxyErrorInfo"},
  637. {810, nullptr, "RegisterProgramIndexMapInfo"},
  638. {1000, nullptr, "SetBisRootForHost"},
  639. {1001, nullptr, "SetSaveDataSize"},
  640. {1002, nullptr, "SetSaveDataRootPath"},
  641. {1003, nullptr, "DisableAutoSaveDataCreation"},
  642. {1004, &FSP_SRV::SetGlobalAccessLogMode, "SetGlobalAccessLogMode"},
  643. {1005, &FSP_SRV::GetGlobalAccessLogMode, "GetGlobalAccessLogMode"},
  644. {1006, &FSP_SRV::OutputAccessLogToSdCard, "OutputAccessLogToSdCard"},
  645. {1007, nullptr, "RegisterUpdatePartition"},
  646. {1008, nullptr, "OpenRegisteredUpdatePartition"},
  647. {1009, nullptr, "GetAndClearMemoryReportInfo"},
  648. {1010, nullptr, "SetDataStorageRedirectTarget"},
  649. {1011, &FSP_SRV::GetProgramIndexForAccessLog, "GetProgramIndexForAccessLog"},
  650. {1012, nullptr, "GetFsStackUsage"},
  651. {1013, nullptr, "UnsetSaveDataRootPath"},
  652. {1014, nullptr, "OutputMultiProgramTagAccessLog"},
  653. {1016, nullptr, "FlushAccessLogOnSdCard"},
  654. {1017, nullptr, "OutputApplicationInfoAccessLog"},
  655. {1100, nullptr, "OverrideSaveDataTransferTokenSignVerificationKey"},
  656. {1110, nullptr, "CorruptSaveDataFileSystemBySaveDataSpaceId2"},
  657. {1200, &FSP_SRV::OpenMultiCommitManager, "OpenMultiCommitManager"},
  658. {1300, nullptr, "OpenBisWiper"},
  659. };
  660. // clang-format on
  661. RegisterHandlers(functions);
  662. }
  663. FSP_SRV::~FSP_SRV() = default;
  664. void FSP_SRV::SetCurrentProcess(Kernel::HLERequestContext& ctx) {
  665. IPC::RequestParser rp{ctx};
  666. current_process_id = rp.Pop<u64>();
  667. LOG_DEBUG(Service_FS, "called. current_process_id=0x{:016X}", current_process_id);
  668. IPC::ResponseBuilder rb{ctx, 2};
  669. rb.Push(RESULT_SUCCESS);
  670. }
  671. void FSP_SRV::OpenFileSystemWithPatch(Kernel::HLERequestContext& ctx) {
  672. IPC::RequestParser rp{ctx};
  673. const auto type = rp.PopRaw<FileSystemType>();
  674. const auto title_id = rp.PopRaw<u64>();
  675. LOG_WARNING(Service_FS, "(STUBBED) called with type={}, title_id={:016X}", type, title_id);
  676. IPC::ResponseBuilder rb{ctx, 2, 0, 0};
  677. rb.Push(RESULT_UNKNOWN);
  678. }
  679. void FSP_SRV::OpenSdCardFileSystem(Kernel::HLERequestContext& ctx) {
  680. LOG_DEBUG(Service_FS, "called");
  681. auto filesystem =
  682. std::make_shared<IFileSystem>(system, fsc.OpenSDMC().Unwrap(),
  683. SizeGetter::FromStorageId(fsc, FileSys::StorageId::SdCard));
  684. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  685. rb.Push(RESULT_SUCCESS);
  686. rb.PushIpcInterface<IFileSystem>(std::move(filesystem));
  687. }
  688. void FSP_SRV::CreateSaveDataFileSystem(Kernel::HLERequestContext& ctx) {
  689. IPC::RequestParser rp{ctx};
  690. auto save_struct = rp.PopRaw<FileSys::SaveDataAttribute>();
  691. [[maybe_unused]] auto save_create_struct = rp.PopRaw<std::array<u8, 0x40>>();
  692. u128 uid = rp.PopRaw<u128>();
  693. LOG_DEBUG(Service_FS, "called save_struct = {}, uid = {:016X}{:016X}", save_struct.DebugInfo(),
  694. uid[1], uid[0]);
  695. fsc.CreateSaveData(FileSys::SaveDataSpaceId::NandUser, save_struct);
  696. IPC::ResponseBuilder rb{ctx, 2};
  697. rb.Push(RESULT_SUCCESS);
  698. }
  699. void FSP_SRV::OpenSaveDataFileSystem(Kernel::HLERequestContext& ctx) {
  700. IPC::RequestParser rp{ctx};
  701. struct Parameters {
  702. FileSys::SaveDataSpaceId space_id;
  703. FileSys::SaveDataAttribute attribute;
  704. };
  705. const auto parameters = rp.PopRaw<Parameters>();
  706. LOG_INFO(Service_FS, "called.");
  707. auto dir = fsc.OpenSaveData(parameters.space_id, parameters.attribute);
  708. if (dir.Failed()) {
  709. IPC::ResponseBuilder rb{ctx, 2, 0, 0};
  710. rb.Push(FileSys::ERROR_ENTITY_NOT_FOUND);
  711. return;
  712. }
  713. FileSys::StorageId id{};
  714. switch (parameters.space_id) {
  715. case FileSys::SaveDataSpaceId::NandUser:
  716. id = FileSys::StorageId::NandUser;
  717. break;
  718. case FileSys::SaveDataSpaceId::SdCardSystem:
  719. case FileSys::SaveDataSpaceId::SdCardUser:
  720. id = FileSys::StorageId::SdCard;
  721. break;
  722. case FileSys::SaveDataSpaceId::NandSystem:
  723. id = FileSys::StorageId::NandSystem;
  724. break;
  725. case FileSys::SaveDataSpaceId::TemporaryStorage:
  726. case FileSys::SaveDataSpaceId::ProperSystem:
  727. case FileSys::SaveDataSpaceId::SafeMode:
  728. UNREACHABLE();
  729. }
  730. auto filesystem = std::make_shared<IFileSystem>(system, std::move(dir.Unwrap()),
  731. SizeGetter::FromStorageId(fsc, id));
  732. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  733. rb.Push(RESULT_SUCCESS);
  734. rb.PushIpcInterface<IFileSystem>(std::move(filesystem));
  735. }
  736. void FSP_SRV::OpenReadOnlySaveDataFileSystem(Kernel::HLERequestContext& ctx) {
  737. LOG_WARNING(Service_FS, "(STUBBED) called, delegating to 51 OpenSaveDataFilesystem");
  738. OpenSaveDataFileSystem(ctx);
  739. }
  740. void FSP_SRV::OpenSaveDataInfoReaderBySaveDataSpaceId(Kernel::HLERequestContext& ctx) {
  741. IPC::RequestParser rp{ctx};
  742. const auto space = rp.PopRaw<FileSys::SaveDataSpaceId>();
  743. LOG_INFO(Service_FS, "called, space={}", space);
  744. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  745. rb.Push(RESULT_SUCCESS);
  746. rb.PushIpcInterface<ISaveDataInfoReader>(
  747. std::make_shared<ISaveDataInfoReader>(system, space, fsc));
  748. }
  749. void FSP_SRV::WriteSaveDataFileSystemExtraDataBySaveDataAttribute(Kernel::HLERequestContext& ctx) {
  750. LOG_WARNING(Service_FS, "(STUBBED) called.");
  751. IPC::ResponseBuilder rb{ctx, 2};
  752. rb.Push(RESULT_SUCCESS);
  753. }
  754. void FSP_SRV::ReadSaveDataFileSystemExtraDataWithMaskBySaveDataAttribute(
  755. Kernel::HLERequestContext& ctx) {
  756. IPC::RequestParser rp{ctx};
  757. struct Parameters {
  758. FileSys::SaveDataSpaceId space_id;
  759. FileSys::SaveDataAttribute attribute;
  760. };
  761. const auto parameters = rp.PopRaw<Parameters>();
  762. // Stub this to None for now, backend needs an impl to read/write the SaveDataExtraData
  763. constexpr auto flags = static_cast<u32>(FileSys::SaveDataFlags::None);
  764. LOG_WARNING(Service_FS,
  765. "(STUBBED) called, flags={}, space_id={}, attribute.title_id={:016X}\n"
  766. "attribute.user_id={:016X}{:016X}, attribute.save_id={:016X}\n"
  767. "attribute.type={}, attribute.rank={}, attribute.index={}",
  768. flags, parameters.space_id, parameters.attribute.title_id,
  769. parameters.attribute.user_id[1], parameters.attribute.user_id[0],
  770. parameters.attribute.save_id, parameters.attribute.type, parameters.attribute.rank,
  771. parameters.attribute.index);
  772. IPC::ResponseBuilder rb{ctx, 3};
  773. rb.Push(RESULT_SUCCESS);
  774. rb.Push(flags);
  775. }
  776. void FSP_SRV::OpenDataStorageByCurrentProcess(Kernel::HLERequestContext& ctx) {
  777. LOG_DEBUG(Service_FS, "called");
  778. auto current_romfs = fsc.OpenRomFSCurrentProcess();
  779. if (current_romfs.Failed()) {
  780. // TODO (bunnei): Find the right error code to use here
  781. LOG_CRITICAL(Service_FS, "no file system interface available!");
  782. IPC::ResponseBuilder rb{ctx, 2};
  783. rb.Push(RESULT_UNKNOWN);
  784. return;
  785. }
  786. auto storage = std::make_shared<IStorage>(system, std::move(current_romfs.Unwrap()));
  787. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  788. rb.Push(RESULT_SUCCESS);
  789. rb.PushIpcInterface<IStorage>(std::move(storage));
  790. }
  791. void FSP_SRV::OpenDataStorageByDataId(Kernel::HLERequestContext& ctx) {
  792. IPC::RequestParser rp{ctx};
  793. const auto storage_id = rp.PopRaw<FileSys::StorageId>();
  794. const auto unknown = rp.PopRaw<u32>();
  795. const auto title_id = rp.PopRaw<u64>();
  796. LOG_DEBUG(Service_FS, "called with storage_id={:02X}, unknown={:08X}, title_id={:016X}",
  797. storage_id, unknown, title_id);
  798. auto data = fsc.OpenRomFS(title_id, storage_id, FileSys::ContentRecordType::Data);
  799. if (data.Failed()) {
  800. const auto archive = FileSys::SystemArchive::SynthesizeSystemArchive(title_id);
  801. if (archive != nullptr) {
  802. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  803. rb.Push(RESULT_SUCCESS);
  804. rb.PushIpcInterface(std::make_shared<IStorage>(system, archive));
  805. return;
  806. }
  807. // TODO(DarkLordZach): Find the right error code to use here
  808. LOG_ERROR(Service_FS,
  809. "could not open data storage with title_id={:016X}, storage_id={:02X}", title_id,
  810. storage_id);
  811. IPC::ResponseBuilder rb{ctx, 2};
  812. rb.Push(RESULT_UNKNOWN);
  813. return;
  814. }
  815. const FileSys::PatchManager pm{title_id, fsc, content_provider};
  816. auto storage = std::make_shared<IStorage>(
  817. system, pm.PatchRomFS(std::move(data.Unwrap()), 0, FileSys::ContentRecordType::Data));
  818. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  819. rb.Push(RESULT_SUCCESS);
  820. rb.PushIpcInterface<IStorage>(std::move(storage));
  821. }
  822. void FSP_SRV::OpenPatchDataStorageByCurrentProcess(Kernel::HLERequestContext& ctx) {
  823. IPC::RequestParser rp{ctx};
  824. const auto storage_id = rp.PopRaw<FileSys::StorageId>();
  825. const auto title_id = rp.PopRaw<u64>();
  826. LOG_DEBUG(Service_FS, "called with storage_id={:02X}, title_id={:016X}", storage_id, title_id);
  827. IPC::ResponseBuilder rb{ctx, 2};
  828. rb.Push(FileSys::ERROR_ENTITY_NOT_FOUND);
  829. }
  830. void FSP_SRV::OpenDataStorageWithProgramIndex(Kernel::HLERequestContext& ctx) {
  831. IPC::RequestParser rp{ctx};
  832. const auto program_index = rp.PopRaw<u8>();
  833. LOG_DEBUG(Service_FS, "called, program_index={}", program_index);
  834. auto patched_romfs = fsc.OpenPatchedRomFSWithProgramIndex(
  835. system.CurrentProcess()->GetTitleID(), program_index, FileSys::ContentRecordType::Program);
  836. if (patched_romfs.Failed()) {
  837. // TODO: Find the right error code to use here
  838. LOG_ERROR(Service_FS, "could not open storage with program_index={}", program_index);
  839. IPC::ResponseBuilder rb{ctx, 2};
  840. rb.Push(RESULT_UNKNOWN);
  841. return;
  842. }
  843. auto storage = std::make_shared<IStorage>(system, std::move(patched_romfs.Unwrap()));
  844. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  845. rb.Push(RESULT_SUCCESS);
  846. rb.PushIpcInterface<IStorage>(std::move(storage));
  847. }
  848. void FSP_SRV::SetGlobalAccessLogMode(Kernel::HLERequestContext& ctx) {
  849. IPC::RequestParser rp{ctx};
  850. log_mode = rp.PopEnum<LogMode>();
  851. LOG_DEBUG(Service_FS, "called, log_mode={:08X}", log_mode);
  852. IPC::ResponseBuilder rb{ctx, 2};
  853. rb.Push(RESULT_SUCCESS);
  854. }
  855. void FSP_SRV::GetGlobalAccessLogMode(Kernel::HLERequestContext& ctx) {
  856. LOG_DEBUG(Service_FS, "called");
  857. IPC::ResponseBuilder rb{ctx, 3};
  858. rb.Push(RESULT_SUCCESS);
  859. rb.PushEnum(log_mode);
  860. }
  861. void FSP_SRV::OutputAccessLogToSdCard(Kernel::HLERequestContext& ctx) {
  862. const auto raw = ctx.ReadBuffer();
  863. auto log = Common::StringFromFixedZeroTerminatedBuffer(
  864. reinterpret_cast<const char*>(raw.data()), raw.size());
  865. LOG_DEBUG(Service_FS, "called, log='{}'", log);
  866. reporter.SaveFilesystemAccessReport(log_mode, std::move(log));
  867. IPC::ResponseBuilder rb{ctx, 2};
  868. rb.Push(RESULT_SUCCESS);
  869. }
  870. void FSP_SRV::GetProgramIndexForAccessLog(Kernel::HLERequestContext& ctx) {
  871. LOG_DEBUG(Service_FS, "called");
  872. IPC::ResponseBuilder rb{ctx, 4};
  873. rb.Push(RESULT_SUCCESS);
  874. rb.PushEnum(AccessLogVersion::Latest);
  875. rb.Push(access_log_program_index);
  876. }
  877. class IMultiCommitManager final : public ServiceFramework<IMultiCommitManager> {
  878. public:
  879. explicit IMultiCommitManager(Core::System& system_)
  880. : ServiceFramework{system_, "IMultiCommitManager"} {
  881. static const FunctionInfo functions[] = {
  882. {1, &IMultiCommitManager::Add, "Add"},
  883. {2, &IMultiCommitManager::Commit, "Commit"},
  884. };
  885. RegisterHandlers(functions);
  886. }
  887. private:
  888. FileSys::VirtualFile backend;
  889. void Add(Kernel::HLERequestContext& ctx) {
  890. LOG_WARNING(Service_FS, "(STUBBED) called");
  891. IPC::ResponseBuilder rb{ctx, 2};
  892. rb.Push(RESULT_SUCCESS);
  893. }
  894. void Commit(Kernel::HLERequestContext& ctx) {
  895. LOG_WARNING(Service_FS, "(STUBBED) called");
  896. IPC::ResponseBuilder rb{ctx, 2};
  897. rb.Push(RESULT_SUCCESS);
  898. }
  899. };
  900. void FSP_SRV::OpenMultiCommitManager(Kernel::HLERequestContext& ctx) {
  901. LOG_DEBUG(Service_FS, "called");
  902. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  903. rb.Push(RESULT_SUCCESS);
  904. rb.PushIpcInterface<IMultiCommitManager>(std::make_shared<IMultiCommitManager>(system));
  905. }
  906. } // namespace Service::FileSystem