fsp_srv.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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/file_sys/directory.h"
  16. #include "core/file_sys/errors.h"
  17. #include "core/file_sys/mode.h"
  18. #include "core/file_sys/nca_metadata.h"
  19. #include "core/file_sys/patch_manager.h"
  20. #include "core/file_sys/savedata_factory.h"
  21. #include "core/file_sys/system_archive/system_archive.h"
  22. #include "core/file_sys/vfs.h"
  23. #include "core/hle/ipc_helpers.h"
  24. #include "core/hle/kernel/process.h"
  25. #include "core/hle/service/filesystem/filesystem.h"
  26. #include "core/hle/service/filesystem/fsp_srv.h"
  27. namespace Service::FileSystem {
  28. enum class FileSystemType : u8 {
  29. Invalid0 = 0,
  30. Invalid1 = 1,
  31. Logo = 2,
  32. ContentControl = 3,
  33. ContentManual = 4,
  34. ContentMeta = 5,
  35. ContentData = 6,
  36. ApplicationPackage = 7,
  37. };
  38. class IStorage final : public ServiceFramework<IStorage> {
  39. public:
  40. explicit IStorage(FileSys::VirtualFile backend_)
  41. : ServiceFramework("IStorage"), backend(std::move(backend_)) {
  42. static const FunctionInfo functions[] = {
  43. {0, &IStorage::Read, "Read"},
  44. {1, nullptr, "Write"},
  45. {2, nullptr, "Flush"},
  46. {3, nullptr, "SetSize"},
  47. {4, &IStorage::GetSize, "GetSize"},
  48. {5, nullptr, "OperateRange"},
  49. };
  50. RegisterHandlers(functions);
  51. }
  52. private:
  53. FileSys::VirtualFile backend;
  54. void Read(Kernel::HLERequestContext& ctx) {
  55. IPC::RequestParser rp{ctx};
  56. const s64 offset = rp.Pop<s64>();
  57. const s64 length = rp.Pop<s64>();
  58. LOG_DEBUG(Service_FS, "called, offset=0x{:X}, length={}", offset, length);
  59. // Error checking
  60. if (length < 0) {
  61. LOG_ERROR(Service_FS, "Length is less than 0, length={}", length);
  62. IPC::ResponseBuilder rb{ctx, 2};
  63. rb.Push(FileSys::ERROR_INVALID_SIZE);
  64. return;
  65. }
  66. if (offset < 0) {
  67. LOG_ERROR(Service_FS, "Offset is less than 0, offset={}", offset);
  68. IPC::ResponseBuilder rb{ctx, 2};
  69. rb.Push(FileSys::ERROR_INVALID_OFFSET);
  70. return;
  71. }
  72. // Read the data from the Storage backend
  73. std::vector<u8> output = backend->ReadBytes(length, offset);
  74. // Write the data to memory
  75. ctx.WriteBuffer(output);
  76. IPC::ResponseBuilder rb{ctx, 2};
  77. rb.Push(RESULT_SUCCESS);
  78. }
  79. void GetSize(Kernel::HLERequestContext& ctx) {
  80. const u64 size = backend->GetSize();
  81. LOG_DEBUG(Service_FS, "called, size={}", size);
  82. IPC::ResponseBuilder rb{ctx, 4};
  83. rb.Push(RESULT_SUCCESS);
  84. rb.Push<u64>(size);
  85. }
  86. };
  87. class IFile final : public ServiceFramework<IFile> {
  88. public:
  89. explicit IFile(FileSys::VirtualFile backend_)
  90. : ServiceFramework("IFile"), backend(std::move(backend_)) {
  91. static const FunctionInfo functions[] = {
  92. {0, &IFile::Read, "Read"}, {1, &IFile::Write, "Write"},
  93. {2, &IFile::Flush, "Flush"}, {3, &IFile::SetSize, "SetSize"},
  94. {4, &IFile::GetSize, "GetSize"}, {5, nullptr, "OperateRange"},
  95. };
  96. RegisterHandlers(functions);
  97. }
  98. private:
  99. FileSys::VirtualFile backend;
  100. void Read(Kernel::HLERequestContext& ctx) {
  101. IPC::RequestParser rp{ctx};
  102. const u64 unk = rp.Pop<u64>();
  103. const s64 offset = rp.Pop<s64>();
  104. const s64 length = rp.Pop<s64>();
  105. LOG_DEBUG(Service_FS, "called, offset=0x{:X}, length={}", offset, length);
  106. // Error checking
  107. if (length < 0) {
  108. LOG_ERROR(Service_FS, "Length is less than 0, length={}", length);
  109. IPC::ResponseBuilder rb{ctx, 2};
  110. rb.Push(FileSys::ERROR_INVALID_SIZE);
  111. return;
  112. }
  113. if (offset < 0) {
  114. LOG_ERROR(Service_FS, "Offset is less than 0, offset={}", offset);
  115. IPC::ResponseBuilder rb{ctx, 2};
  116. rb.Push(FileSys::ERROR_INVALID_OFFSET);
  117. return;
  118. }
  119. // Read the data from the Storage backend
  120. std::vector<u8> output = backend->ReadBytes(length, offset);
  121. // Write the data to memory
  122. ctx.WriteBuffer(output);
  123. IPC::ResponseBuilder rb{ctx, 4};
  124. rb.Push(RESULT_SUCCESS);
  125. rb.Push(static_cast<u64>(output.size()));
  126. }
  127. void Write(Kernel::HLERequestContext& ctx) {
  128. IPC::RequestParser rp{ctx};
  129. const u64 unk = rp.Pop<u64>();
  130. const s64 offset = rp.Pop<s64>();
  131. const s64 length = rp.Pop<s64>();
  132. LOG_DEBUG(Service_FS, "called, offset=0x{:X}, length={}", offset, length);
  133. // Error checking
  134. if (length < 0) {
  135. LOG_ERROR(Service_FS, "Length is less than 0, length={}", length);
  136. IPC::ResponseBuilder rb{ctx, 2};
  137. rb.Push(FileSys::ERROR_INVALID_SIZE);
  138. return;
  139. }
  140. if (offset < 0) {
  141. LOG_ERROR(Service_FS, "Offset is less than 0, offset={}", offset);
  142. IPC::ResponseBuilder rb{ctx, 2};
  143. rb.Push(FileSys::ERROR_INVALID_OFFSET);
  144. return;
  145. }
  146. const std::vector<u8> data = ctx.ReadBuffer();
  147. ASSERT_MSG(
  148. static_cast<s64>(data.size()) <= length,
  149. "Attempting to write more data than requested (requested={:016X}, actual={:016X}).",
  150. length, data.size());
  151. // Write the data to the Storage backend
  152. const auto write_size =
  153. static_cast<std::size_t>(std::distance(data.begin(), data.begin() + length));
  154. const std::size_t written = backend->Write(data.data(), write_size, offset);
  155. ASSERT_MSG(static_cast<s64>(written) == length,
  156. "Could not write all bytes to file (requested={:016X}, actual={:016X}).", length,
  157. written);
  158. IPC::ResponseBuilder rb{ctx, 2};
  159. rb.Push(RESULT_SUCCESS);
  160. }
  161. void Flush(Kernel::HLERequestContext& ctx) {
  162. LOG_DEBUG(Service_FS, "called");
  163. // Exists for SDK compatibiltity -- No need to flush file.
  164. IPC::ResponseBuilder rb{ctx, 2};
  165. rb.Push(RESULT_SUCCESS);
  166. }
  167. void SetSize(Kernel::HLERequestContext& ctx) {
  168. IPC::RequestParser rp{ctx};
  169. const u64 size = rp.Pop<u64>();
  170. LOG_DEBUG(Service_FS, "called, size={}", size);
  171. backend->Resize(size);
  172. IPC::ResponseBuilder rb{ctx, 2};
  173. rb.Push(RESULT_SUCCESS);
  174. }
  175. void GetSize(Kernel::HLERequestContext& ctx) {
  176. const u64 size = backend->GetSize();
  177. LOG_DEBUG(Service_FS, "called, size={}", size);
  178. IPC::ResponseBuilder rb{ctx, 4};
  179. rb.Push(RESULT_SUCCESS);
  180. rb.Push<u64>(size);
  181. }
  182. };
  183. template <typename T>
  184. static void BuildEntryIndex(std::vector<FileSys::Entry>& entries, const std::vector<T>& new_data,
  185. FileSys::EntryType type) {
  186. entries.reserve(entries.size() + new_data.size());
  187. for (const auto& new_entry : new_data) {
  188. entries.emplace_back(new_entry->GetName(), type, new_entry->GetSize());
  189. }
  190. }
  191. class IDirectory final : public ServiceFramework<IDirectory> {
  192. public:
  193. explicit IDirectory(FileSys::VirtualDir backend_)
  194. : ServiceFramework("IDirectory"), backend(std::move(backend_)) {
  195. static const FunctionInfo functions[] = {
  196. {0, &IDirectory::Read, "Read"},
  197. {1, &IDirectory::GetEntryCount, "GetEntryCount"},
  198. };
  199. RegisterHandlers(functions);
  200. // TODO(DarkLordZach): Verify that this is the correct behavior.
  201. // Build entry index now to save time later.
  202. BuildEntryIndex(entries, backend->GetFiles(), FileSys::File);
  203. BuildEntryIndex(entries, backend->GetSubdirectories(), FileSys::Directory);
  204. }
  205. private:
  206. FileSys::VirtualDir backend;
  207. std::vector<FileSys::Entry> entries;
  208. u64 next_entry_index = 0;
  209. void Read(Kernel::HLERequestContext& ctx) {
  210. IPC::RequestParser rp{ctx};
  211. const u64 unk = rp.Pop<u64>();
  212. LOG_DEBUG(Service_FS, "called, unk=0x{:X}", unk);
  213. // Calculate how many entries we can fit in the output buffer
  214. const u64 count_entries = ctx.GetWriteBufferSize() / sizeof(FileSys::Entry);
  215. // Cap at total number of entries.
  216. const u64 actual_entries = std::min(count_entries, entries.size() - next_entry_index);
  217. // Determine data start and end
  218. const auto* begin = reinterpret_cast<u8*>(entries.data() + next_entry_index);
  219. const auto* end = reinterpret_cast<u8*>(entries.data() + next_entry_index + actual_entries);
  220. const auto range_size = static_cast<std::size_t>(std::distance(begin, end));
  221. next_entry_index += actual_entries;
  222. // Write the data to memory
  223. ctx.WriteBuffer(begin, range_size);
  224. IPC::ResponseBuilder rb{ctx, 4};
  225. rb.Push(RESULT_SUCCESS);
  226. rb.Push(actual_entries);
  227. }
  228. void GetEntryCount(Kernel::HLERequestContext& ctx) {
  229. LOG_DEBUG(Service_FS, "called");
  230. u64 count = entries.size() - next_entry_index;
  231. IPC::ResponseBuilder rb{ctx, 4};
  232. rb.Push(RESULT_SUCCESS);
  233. rb.Push(count);
  234. }
  235. };
  236. class IFileSystem final : public ServiceFramework<IFileSystem> {
  237. public:
  238. explicit IFileSystem(FileSys::VirtualDir backend)
  239. : ServiceFramework("IFileSystem"), backend(std::move(backend)) {
  240. static const FunctionInfo functions[] = {
  241. {0, &IFileSystem::CreateFile, "CreateFile"},
  242. {1, &IFileSystem::DeleteFile, "DeleteFile"},
  243. {2, &IFileSystem::CreateDirectory, "CreateDirectory"},
  244. {3, &IFileSystem::DeleteDirectory, "DeleteDirectory"},
  245. {4, &IFileSystem::DeleteDirectoryRecursively, "DeleteDirectoryRecursively"},
  246. {5, &IFileSystem::RenameFile, "RenameFile"},
  247. {6, nullptr, "RenameDirectory"},
  248. {7, &IFileSystem::GetEntryType, "GetEntryType"},
  249. {8, &IFileSystem::OpenFile, "OpenFile"},
  250. {9, &IFileSystem::OpenDirectory, "OpenDirectory"},
  251. {10, &IFileSystem::Commit, "Commit"},
  252. {11, nullptr, "GetFreeSpaceSize"},
  253. {12, nullptr, "GetTotalSpaceSize"},
  254. {13, &IFileSystem::CleanDirectoryRecursively, "CleanDirectoryRecursively"},
  255. {14, nullptr, "GetFileTimeStampRaw"},
  256. {15, nullptr, "QueryEntry"},
  257. };
  258. RegisterHandlers(functions);
  259. }
  260. void CreateFile(Kernel::HLERequestContext& ctx) {
  261. IPC::RequestParser rp{ctx};
  262. auto file_buffer = ctx.ReadBuffer();
  263. std::string name = Common::StringFromBuffer(file_buffer);
  264. u64 mode = rp.Pop<u64>();
  265. u32 size = rp.Pop<u32>();
  266. LOG_DEBUG(Service_FS, "called file {} mode 0x{:X} size 0x{:08X}", name, mode, size);
  267. IPC::ResponseBuilder rb{ctx, 2};
  268. rb.Push(backend.CreateFile(name, size));
  269. }
  270. void DeleteFile(Kernel::HLERequestContext& ctx) {
  271. IPC::RequestParser rp{ctx};
  272. auto file_buffer = ctx.ReadBuffer();
  273. std::string name = Common::StringFromBuffer(file_buffer);
  274. LOG_DEBUG(Service_FS, "called file {}", name);
  275. IPC::ResponseBuilder rb{ctx, 2};
  276. rb.Push(backend.DeleteFile(name));
  277. }
  278. void CreateDirectory(Kernel::HLERequestContext& ctx) {
  279. IPC::RequestParser rp{ctx};
  280. auto file_buffer = ctx.ReadBuffer();
  281. std::string name = Common::StringFromBuffer(file_buffer);
  282. LOG_DEBUG(Service_FS, "called directory {}", name);
  283. IPC::ResponseBuilder rb{ctx, 2};
  284. rb.Push(backend.CreateDirectory(name));
  285. }
  286. void DeleteDirectory(Kernel::HLERequestContext& ctx) {
  287. const IPC::RequestParser rp{ctx};
  288. const auto file_buffer = ctx.ReadBuffer();
  289. std::string name = Common::StringFromBuffer(file_buffer);
  290. LOG_DEBUG(Service_FS, "called directory {}", name);
  291. IPC::ResponseBuilder rb{ctx, 2};
  292. rb.Push(backend.DeleteDirectory(name));
  293. }
  294. void DeleteDirectoryRecursively(Kernel::HLERequestContext& ctx) {
  295. const IPC::RequestParser rp{ctx};
  296. const auto file_buffer = ctx.ReadBuffer();
  297. std::string name = Common::StringFromBuffer(file_buffer);
  298. LOG_DEBUG(Service_FS, "called directory {}", name);
  299. IPC::ResponseBuilder rb{ctx, 2};
  300. rb.Push(backend.DeleteDirectoryRecursively(name));
  301. }
  302. void CleanDirectoryRecursively(Kernel::HLERequestContext& ctx) {
  303. const auto file_buffer = ctx.ReadBuffer();
  304. const std::string name = Common::StringFromBuffer(file_buffer);
  305. LOG_DEBUG(Service_FS, "called. Directory: {}", name);
  306. IPC::ResponseBuilder rb{ctx, 2};
  307. rb.Push(backend.CleanDirectoryRecursively(name));
  308. }
  309. void RenameFile(Kernel::HLERequestContext& ctx) {
  310. IPC::RequestParser rp{ctx};
  311. std::vector<u8> buffer;
  312. buffer.resize(ctx.BufferDescriptorX()[0].Size());
  313. Memory::ReadBlock(ctx.BufferDescriptorX()[0].Address(), buffer.data(), buffer.size());
  314. std::string src_name = Common::StringFromBuffer(buffer);
  315. buffer.resize(ctx.BufferDescriptorX()[1].Size());
  316. Memory::ReadBlock(ctx.BufferDescriptorX()[1].Address(), buffer.data(), buffer.size());
  317. std::string dst_name = Common::StringFromBuffer(buffer);
  318. LOG_DEBUG(Service_FS, "called file '{}' to file '{}'", src_name, dst_name);
  319. IPC::ResponseBuilder rb{ctx, 2};
  320. rb.Push(backend.RenameFile(src_name, dst_name));
  321. }
  322. void OpenFile(Kernel::HLERequestContext& ctx) {
  323. IPC::RequestParser rp{ctx};
  324. auto file_buffer = ctx.ReadBuffer();
  325. std::string name = Common::StringFromBuffer(file_buffer);
  326. auto mode = static_cast<FileSys::Mode>(rp.Pop<u32>());
  327. LOG_DEBUG(Service_FS, "called file {} mode {}", name, static_cast<u32>(mode));
  328. auto result = backend.OpenFile(name, mode);
  329. if (result.Failed()) {
  330. IPC::ResponseBuilder rb{ctx, 2};
  331. rb.Push(result.Code());
  332. return;
  333. }
  334. IFile file(result.Unwrap());
  335. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  336. rb.Push(RESULT_SUCCESS);
  337. rb.PushIpcInterface<IFile>(std::move(file));
  338. }
  339. void OpenDirectory(Kernel::HLERequestContext& ctx) {
  340. IPC::RequestParser rp{ctx};
  341. auto file_buffer = ctx.ReadBuffer();
  342. std::string name = Common::StringFromBuffer(file_buffer);
  343. // TODO(Subv): Implement this filter.
  344. u32 filter_flags = rp.Pop<u32>();
  345. LOG_DEBUG(Service_FS, "called directory {} filter {}", name, filter_flags);
  346. auto result = backend.OpenDirectory(name);
  347. if (result.Failed()) {
  348. IPC::ResponseBuilder rb{ctx, 2};
  349. rb.Push(result.Code());
  350. return;
  351. }
  352. IDirectory directory(result.Unwrap());
  353. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  354. rb.Push(RESULT_SUCCESS);
  355. rb.PushIpcInterface<IDirectory>(std::move(directory));
  356. }
  357. void GetEntryType(Kernel::HLERequestContext& ctx) {
  358. IPC::RequestParser rp{ctx};
  359. auto file_buffer = ctx.ReadBuffer();
  360. std::string name = Common::StringFromBuffer(file_buffer);
  361. LOG_DEBUG(Service_FS, "called file {}", name);
  362. auto result = backend.GetEntryType(name);
  363. if (result.Failed()) {
  364. IPC::ResponseBuilder rb{ctx, 2};
  365. rb.Push(result.Code());
  366. return;
  367. }
  368. IPC::ResponseBuilder rb{ctx, 3};
  369. rb.Push(RESULT_SUCCESS);
  370. rb.Push<u32>(static_cast<u32>(*result));
  371. }
  372. void Commit(Kernel::HLERequestContext& ctx) {
  373. LOG_WARNING(Service_FS, "(STUBBED) called");
  374. IPC::ResponseBuilder rb{ctx, 2};
  375. rb.Push(RESULT_SUCCESS);
  376. }
  377. private:
  378. VfsDirectoryServiceWrapper backend;
  379. };
  380. class ISaveDataInfoReader final : public ServiceFramework<ISaveDataInfoReader> {
  381. public:
  382. explicit ISaveDataInfoReader(FileSys::SaveDataSpaceId space)
  383. : ServiceFramework("ISaveDataInfoReader") {
  384. static const FunctionInfo functions[] = {
  385. {0, &ISaveDataInfoReader::ReadSaveDataInfo, "ReadSaveDataInfo"},
  386. };
  387. RegisterHandlers(functions);
  388. FindAllSaves(space);
  389. }
  390. void ReadSaveDataInfo(Kernel::HLERequestContext& ctx) {
  391. LOG_DEBUG(Service_FS, "called");
  392. // Calculate how many entries we can fit in the output buffer
  393. const u64 count_entries = ctx.GetWriteBufferSize() / sizeof(SaveDataInfo);
  394. // Cap at total number of entries.
  395. const u64 actual_entries = std::min(count_entries, info.size() - next_entry_index);
  396. // Determine data start and end
  397. const auto* begin = reinterpret_cast<u8*>(info.data() + next_entry_index);
  398. const auto* end = reinterpret_cast<u8*>(info.data() + next_entry_index + actual_entries);
  399. const auto range_size = static_cast<std::size_t>(std::distance(begin, end));
  400. next_entry_index += actual_entries;
  401. // Write the data to memory
  402. ctx.WriteBuffer(begin, range_size);
  403. IPC::ResponseBuilder rb{ctx, 3};
  404. rb.Push(RESULT_SUCCESS);
  405. rb.Push<u32>(static_cast<u32>(actual_entries));
  406. }
  407. private:
  408. static u64 stoull_be(std::string_view str) {
  409. if (str.size() != 16)
  410. return 0;
  411. const auto bytes = Common::HexStringToArray<0x8>(str);
  412. u64 out{};
  413. std::memcpy(&out, bytes.data(), sizeof(u64));
  414. return Common::swap64(out);
  415. }
  416. void FindAllSaves(FileSys::SaveDataSpaceId space) {
  417. const auto save_root = OpenSaveDataSpace(space);
  418. ASSERT(save_root.Succeeded());
  419. for (const auto& type : (*save_root)->GetSubdirectories()) {
  420. if (type->GetName() == "save") {
  421. for (const auto& save_id : type->GetSubdirectories()) {
  422. for (const auto& user_id : save_id->GetSubdirectories()) {
  423. const auto save_id_numeric = stoull_be(save_id->GetName());
  424. auto user_id_numeric = Common::HexStringToArray<0x10>(user_id->GetName());
  425. std::reverse(user_id_numeric.begin(), user_id_numeric.end());
  426. if (save_id_numeric != 0) {
  427. // System Save Data
  428. info.emplace_back(SaveDataInfo{
  429. 0,
  430. space,
  431. FileSys::SaveDataType::SystemSaveData,
  432. {},
  433. user_id_numeric,
  434. save_id_numeric,
  435. 0,
  436. user_id->GetSize(),
  437. {},
  438. });
  439. continue;
  440. }
  441. for (const auto& title_id : user_id->GetSubdirectories()) {
  442. const auto device =
  443. std::all_of(user_id_numeric.begin(), user_id_numeric.end(),
  444. [](u8 val) { return val == 0; });
  445. info.emplace_back(SaveDataInfo{
  446. 0,
  447. space,
  448. device ? FileSys::SaveDataType::DeviceSaveData
  449. : FileSys::SaveDataType::SaveData,
  450. {},
  451. user_id_numeric,
  452. save_id_numeric,
  453. stoull_be(title_id->GetName()),
  454. title_id->GetSize(),
  455. {},
  456. });
  457. }
  458. }
  459. }
  460. } else if (space == FileSys::SaveDataSpaceId::TemporaryStorage) {
  461. // Temporary Storage
  462. for (const auto& user_id : type->GetSubdirectories()) {
  463. for (const auto& title_id : user_id->GetSubdirectories()) {
  464. if (!title_id->GetFiles().empty() ||
  465. !title_id->GetSubdirectories().empty()) {
  466. auto user_id_numeric =
  467. Common::HexStringToArray<0x10>(user_id->GetName());
  468. std::reverse(user_id_numeric.begin(), user_id_numeric.end());
  469. info.emplace_back(SaveDataInfo{
  470. 0,
  471. space,
  472. FileSys::SaveDataType::TemporaryStorage,
  473. {},
  474. user_id_numeric,
  475. stoull_be(type->GetName()),
  476. stoull_be(title_id->GetName()),
  477. title_id->GetSize(),
  478. {},
  479. });
  480. }
  481. }
  482. }
  483. }
  484. }
  485. }
  486. struct SaveDataInfo {
  487. u64_le save_id_unknown;
  488. FileSys::SaveDataSpaceId space;
  489. FileSys::SaveDataType type;
  490. INSERT_PADDING_BYTES(0x6);
  491. std::array<u8, 0x10> user_id;
  492. u64_le save_id;
  493. u64_le title_id;
  494. u64_le save_image_size;
  495. INSERT_PADDING_BYTES(0x28);
  496. };
  497. static_assert(sizeof(SaveDataInfo) == 0x60, "SaveDataInfo has incorrect size.");
  498. std::vector<SaveDataInfo> info;
  499. u64 next_entry_index = 0;
  500. };
  501. FSP_SRV::FSP_SRV() : ServiceFramework("fsp-srv") {
  502. // clang-format off
  503. static const FunctionInfo functions[] = {
  504. {0, nullptr, "MountContent"},
  505. {1, &FSP_SRV::Initialize, "Initialize"},
  506. {2, nullptr, "OpenDataFileSystemByCurrentProcess"},
  507. {7, &FSP_SRV::OpenFileSystemWithPatch, "OpenFileSystemWithPatch"},
  508. {8, nullptr, "OpenFileSystemWithId"},
  509. {9, nullptr, "OpenDataFileSystemByApplicationId"},
  510. {11, nullptr, "OpenBisFileSystem"},
  511. {12, nullptr, "OpenBisStorage"},
  512. {13, nullptr, "InvalidateBisCache"},
  513. {17, nullptr, "OpenHostFileSystem"},
  514. {18, &FSP_SRV::MountSdCard, "MountSdCard"},
  515. {19, nullptr, "FormatSdCardFileSystem"},
  516. {21, nullptr, "DeleteSaveDataFileSystem"},
  517. {22, &FSP_SRV::CreateSaveData, "CreateSaveData"},
  518. {23, nullptr, "CreateSaveDataFileSystemBySystemSaveDataId"},
  519. {24, nullptr, "RegisterSaveDataFileSystemAtomicDeletion"},
  520. {25, nullptr, "DeleteSaveDataFileSystemBySaveDataSpaceId"},
  521. {26, nullptr, "FormatSdCardDryRun"},
  522. {27, nullptr, "IsExFatSupported"},
  523. {28, nullptr, "DeleteSaveDataFileSystemBySaveDataAttribute"},
  524. {30, nullptr, "OpenGameCardStorage"},
  525. {31, nullptr, "OpenGameCardFileSystem"},
  526. {32, nullptr, "ExtendSaveDataFileSystem"},
  527. {33, nullptr, "DeleteCacheStorage"},
  528. {34, nullptr, "GetCacheStorageSize"},
  529. {51, &FSP_SRV::MountSaveData, "MountSaveData"},
  530. {52, nullptr, "OpenSaveDataFileSystemBySystemSaveDataId"},
  531. {53, &FSP_SRV::OpenReadOnlySaveDataFileSystem, "OpenReadOnlySaveDataFileSystem"},
  532. {57, nullptr, "ReadSaveDataFileSystemExtraDataBySaveDataSpaceId"},
  533. {58, nullptr, "ReadSaveDataFileSystemExtraData"},
  534. {59, nullptr, "WriteSaveDataFileSystemExtraData"},
  535. {60, nullptr, "OpenSaveDataInfoReader"},
  536. {61, &FSP_SRV::OpenSaveDataInfoReaderBySaveDataSpaceId, "OpenSaveDataInfoReaderBySaveDataSpaceId"},
  537. {62, nullptr, "OpenCacheStorageList"},
  538. {64, nullptr, "OpenSaveDataInternalStorageFileSystem"},
  539. {65, nullptr, "UpdateSaveDataMacForDebug"},
  540. {66, nullptr, "WriteSaveDataFileSystemExtraData2"},
  541. {80, nullptr, "OpenSaveDataMetaFile"},
  542. {81, nullptr, "OpenSaveDataTransferManager"},
  543. {82, nullptr, "OpenSaveDataTransferManagerVersion2"},
  544. {83, nullptr, "OpenSaveDataTransferProhibiterForCloudBackUp"},
  545. {100, nullptr, "OpenImageDirectoryFileSystem"},
  546. {110, nullptr, "OpenContentStorageFileSystem"},
  547. {200, &FSP_SRV::OpenDataStorageByCurrentProcess, "OpenDataStorageByCurrentProcess"},
  548. {201, nullptr, "OpenDataStorageByProgramId"},
  549. {202, &FSP_SRV::OpenDataStorageByDataId, "OpenDataStorageByDataId"},
  550. {203, &FSP_SRV::OpenRomStorage, "OpenRomStorage"},
  551. {400, nullptr, "OpenDeviceOperator"},
  552. {500, nullptr, "OpenSdCardDetectionEventNotifier"},
  553. {501, nullptr, "OpenGameCardDetectionEventNotifier"},
  554. {510, nullptr, "OpenSystemDataUpdateEventNotifier"},
  555. {511, nullptr, "NotifySystemDataUpdateEvent"},
  556. {600, nullptr, "SetCurrentPosixTime"},
  557. {601, nullptr, "QuerySaveDataTotalSize"},
  558. {602, nullptr, "VerifySaveDataFileSystem"},
  559. {603, nullptr, "CorruptSaveDataFileSystem"},
  560. {604, nullptr, "CreatePaddingFile"},
  561. {605, nullptr, "DeleteAllPaddingFiles"},
  562. {606, nullptr, "GetRightsId"},
  563. {607, nullptr, "RegisterExternalKey"},
  564. {608, nullptr, "UnregisterAllExternalKey"},
  565. {609, nullptr, "GetRightsIdByPath"},
  566. {610, nullptr, "GetRightsIdAndKeyGenerationByPath"},
  567. {611, nullptr, "SetCurrentPosixTimeWithTimeDifference"},
  568. {612, nullptr, "GetFreeSpaceSizeForSaveData"},
  569. {613, nullptr, "VerifySaveDataFileSystemBySaveDataSpaceId"},
  570. {614, nullptr, "CorruptSaveDataFileSystemBySaveDataSpaceId"},
  571. {615, nullptr, "QuerySaveDataInternalStorageTotalSize"},
  572. {616, nullptr, "GetSaveDataCommitId"},
  573. {620, nullptr, "SetSdCardEncryptionSeed"},
  574. {630, nullptr, "SetSdCardAccessibility"},
  575. {631, nullptr, "IsSdCardAccessible"},
  576. {640, nullptr, "IsSignedSystemPartitionOnSdCardValid"},
  577. {700, nullptr, "OpenAccessFailureResolver"},
  578. {701, nullptr, "GetAccessFailureDetectionEvent"},
  579. {702, nullptr, "IsAccessFailureDetected"},
  580. {710, nullptr, "ResolveAccessFailure"},
  581. {720, nullptr, "AbandonAccessFailure"},
  582. {800, nullptr, "GetAndClearFileSystemProxyErrorInfo"},
  583. {1000, nullptr, "SetBisRootForHost"},
  584. {1001, nullptr, "SetSaveDataSize"},
  585. {1002, nullptr, "SetSaveDataRootPath"},
  586. {1003, nullptr, "DisableAutoSaveDataCreation"},
  587. {1004, nullptr, "SetGlobalAccessLogMode"},
  588. {1005, &FSP_SRV::GetGlobalAccessLogMode, "GetGlobalAccessLogMode"},
  589. {1006, nullptr, "OutputAccessLogToSdCard"},
  590. {1007, nullptr, "RegisterUpdatePartition"},
  591. {1008, nullptr, "OpenRegisteredUpdatePartition"},
  592. {1009, nullptr, "GetAndClearMemoryReportInfo"},
  593. {1100, nullptr, "OverrideSaveDataTransferTokenSignVerificationKey"},
  594. };
  595. // clang-format on
  596. RegisterHandlers(functions);
  597. }
  598. FSP_SRV::~FSP_SRV() = default;
  599. void FSP_SRV::Initialize(Kernel::HLERequestContext& ctx) {
  600. LOG_WARNING(Service_FS, "(STUBBED) called");
  601. IPC::ResponseBuilder rb{ctx, 2};
  602. rb.Push(RESULT_SUCCESS);
  603. }
  604. void FSP_SRV::OpenFileSystemWithPatch(Kernel::HLERequestContext& ctx) {
  605. IPC::RequestParser rp{ctx};
  606. const auto type = rp.PopRaw<FileSystemType>();
  607. const auto title_id = rp.PopRaw<u64>();
  608. LOG_WARNING(Service_FS, "(STUBBED) called with type={}, title_id={:016X}",
  609. static_cast<u8>(type), title_id);
  610. IPC::ResponseBuilder rb{ctx, 2, 0, 0};
  611. rb.Push(ResultCode(-1));
  612. }
  613. void FSP_SRV::MountSdCard(Kernel::HLERequestContext& ctx) {
  614. LOG_DEBUG(Service_FS, "called");
  615. IFileSystem filesystem(OpenSDMC().Unwrap());
  616. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  617. rb.Push(RESULT_SUCCESS);
  618. rb.PushIpcInterface<IFileSystem>(std::move(filesystem));
  619. }
  620. void FSP_SRV::CreateSaveData(Kernel::HLERequestContext& ctx) {
  621. IPC::RequestParser rp{ctx};
  622. auto save_struct = rp.PopRaw<FileSys::SaveDataDescriptor>();
  623. auto save_create_struct = rp.PopRaw<std::array<u8, 0x40>>();
  624. u128 uid = rp.PopRaw<u128>();
  625. LOG_WARNING(Service_FS, "(STUBBED) called save_struct = {}, uid = {:016X}{:016X}",
  626. save_struct.DebugInfo(), uid[1], uid[0]);
  627. IPC::ResponseBuilder rb{ctx, 2};
  628. rb.Push(RESULT_SUCCESS);
  629. }
  630. void FSP_SRV::MountSaveData(Kernel::HLERequestContext& ctx) {
  631. IPC::RequestParser rp{ctx};
  632. auto space_id = rp.PopRaw<FileSys::SaveDataSpaceId>();
  633. auto unk = rp.Pop<u32>();
  634. LOG_INFO(Service_FS, "called with unknown={:08X}", unk);
  635. auto save_struct = rp.PopRaw<FileSys::SaveDataDescriptor>();
  636. auto dir = OpenSaveData(space_id, save_struct);
  637. if (dir.Failed()) {
  638. IPC::ResponseBuilder rb{ctx, 2, 0, 0};
  639. rb.Push(FileSys::ERROR_ENTITY_NOT_FOUND);
  640. return;
  641. }
  642. IFileSystem filesystem(std::move(dir.Unwrap()));
  643. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  644. rb.Push(RESULT_SUCCESS);
  645. rb.PushIpcInterface<IFileSystem>(std::move(filesystem));
  646. }
  647. void FSP_SRV::OpenReadOnlySaveDataFileSystem(Kernel::HLERequestContext& ctx) {
  648. LOG_WARNING(Service_FS, "(STUBBED) called, delegating to 51 OpenSaveDataFilesystem");
  649. MountSaveData(ctx);
  650. }
  651. void FSP_SRV::OpenSaveDataInfoReaderBySaveDataSpaceId(Kernel::HLERequestContext& ctx) {
  652. IPC::RequestParser rp{ctx};
  653. const auto space = rp.PopRaw<FileSys::SaveDataSpaceId>();
  654. LOG_INFO(Service_FS, "called, space={}", static_cast<u8>(space));
  655. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  656. rb.Push(RESULT_SUCCESS);
  657. rb.PushIpcInterface<ISaveDataInfoReader>(std::make_shared<ISaveDataInfoReader>(space));
  658. }
  659. void FSP_SRV::GetGlobalAccessLogMode(Kernel::HLERequestContext& ctx) {
  660. LOG_WARNING(Service_FS, "(STUBBED) called");
  661. enum class LogMode : u32 {
  662. Off,
  663. Log,
  664. RedirectToSdCard,
  665. LogToSdCard = Log | RedirectToSdCard,
  666. };
  667. // Given we always want to receive logging information,
  668. // we always specify logging as enabled.
  669. IPC::ResponseBuilder rb{ctx, 3};
  670. rb.Push(RESULT_SUCCESS);
  671. rb.PushEnum(LogMode::Log);
  672. }
  673. void FSP_SRV::OpenDataStorageByCurrentProcess(Kernel::HLERequestContext& ctx) {
  674. LOG_DEBUG(Service_FS, "called");
  675. auto romfs = OpenRomFSCurrentProcess();
  676. if (romfs.Failed()) {
  677. // TODO (bunnei): Find the right error code to use here
  678. LOG_CRITICAL(Service_FS, "no file system interface available!");
  679. IPC::ResponseBuilder rb{ctx, 2};
  680. rb.Push(ResultCode(-1));
  681. return;
  682. }
  683. IStorage storage(std::move(romfs.Unwrap()));
  684. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  685. rb.Push(RESULT_SUCCESS);
  686. rb.PushIpcInterface<IStorage>(std::move(storage));
  687. }
  688. void FSP_SRV::OpenDataStorageByDataId(Kernel::HLERequestContext& ctx) {
  689. IPC::RequestParser rp{ctx};
  690. const auto storage_id = rp.PopRaw<FileSys::StorageId>();
  691. const auto unknown = rp.PopRaw<u32>();
  692. const auto title_id = rp.PopRaw<u64>();
  693. LOG_DEBUG(Service_FS, "called with storage_id={:02X}, unknown={:08X}, title_id={:016X}",
  694. static_cast<u8>(storage_id), unknown, title_id);
  695. auto data = OpenRomFS(title_id, storage_id, FileSys::ContentRecordType::Data);
  696. if (data.Failed()) {
  697. const auto archive = FileSys::SystemArchive::SynthesizeSystemArchive(title_id);
  698. if (archive != nullptr) {
  699. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  700. rb.Push(RESULT_SUCCESS);
  701. rb.PushIpcInterface(std::make_shared<IStorage>(archive));
  702. return;
  703. }
  704. // TODO(DarkLordZach): Find the right error code to use here
  705. LOG_ERROR(Service_FS,
  706. "could not open data storage with title_id={:016X}, storage_id={:02X}", title_id,
  707. static_cast<u8>(storage_id));
  708. IPC::ResponseBuilder rb{ctx, 2};
  709. rb.Push(ResultCode(-1));
  710. return;
  711. }
  712. FileSys::PatchManager pm{title_id};
  713. IStorage storage(pm.PatchRomFS(std::move(data.Unwrap()), 0, FileSys::ContentRecordType::Data));
  714. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  715. rb.Push(RESULT_SUCCESS);
  716. rb.PushIpcInterface<IStorage>(std::move(storage));
  717. }
  718. void FSP_SRV::OpenRomStorage(Kernel::HLERequestContext& ctx) {
  719. IPC::RequestParser rp{ctx};
  720. auto storage_id = rp.PopRaw<FileSys::StorageId>();
  721. auto title_id = rp.PopRaw<u64>();
  722. LOG_DEBUG(Service_FS, "called with storage_id={:02X}, title_id={:016X}",
  723. static_cast<u8>(storage_id), title_id);
  724. IPC::ResponseBuilder rb{ctx, 2};
  725. rb.Push(FileSys::ERROR_ENTITY_NOT_FOUND);
  726. }
  727. } // namespace Service::FileSystem