fsp_srv.cpp 42 KB

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