IFileSystemProxy.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. using LibHac;
  2. using LibHac.Fs;
  3. using LibHac.FsService;
  4. using LibHac.FsSystem;
  5. using LibHac.FsSystem.NcaUtils;
  6. using LibHac.Ncm;
  7. using Ryujinx.Common;
  8. using Ryujinx.Common.Logging;
  9. using Ryujinx.Cpu;
  10. using Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy;
  11. using System.IO;
  12. using static Ryujinx.HLE.Utilities.StringUtils;
  13. using StorageId = Ryujinx.HLE.FileSystem.StorageId;
  14. namespace Ryujinx.HLE.HOS.Services.Fs
  15. {
  16. [Service("fsp-srv")]
  17. class IFileSystemProxy : IpcService
  18. {
  19. private LibHac.FsService.IFileSystemProxy _baseFileSystemProxy;
  20. public IFileSystemProxy(ServiceCtx context)
  21. {
  22. _baseFileSystemProxy = context.Device.FileSystem.FsServer.CreateFileSystemProxyService();
  23. }
  24. [Command(1)]
  25. // Initialize(u64, pid)
  26. public ResultCode Initialize(ServiceCtx context)
  27. {
  28. return ResultCode.Success;
  29. }
  30. [Command(8)]
  31. // OpenFileSystemWithId(nn::fssrv::sf::FileSystemType filesystem_type, nn::ApplicationId tid, buffer<bytes<0x301>, 0x19, 0x301> path)
  32. // -> object<nn::fssrv::sf::IFileSystem> contentFs
  33. public ResultCode OpenFileSystemWithId(ServiceCtx context)
  34. {
  35. FileSystemType fileSystemType = (FileSystemType)context.RequestData.ReadInt32();
  36. long titleId = context.RequestData.ReadInt64();
  37. string switchPath = ReadUtf8String(context);
  38. string fullPath = context.Device.FileSystem.SwitchPathToSystemPath(switchPath);
  39. if (!File.Exists(fullPath))
  40. {
  41. if (fullPath.Contains("."))
  42. {
  43. ResultCode result = FileSystemProxyHelper.OpenFileSystemFromInternalFile(context, fullPath, out FileSystemProxy.IFileSystem fileSystem);
  44. if (result == ResultCode.Success)
  45. {
  46. MakeObject(context, fileSystem);
  47. }
  48. return result;
  49. }
  50. return ResultCode.PathDoesNotExist;
  51. }
  52. FileStream fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
  53. string extension = Path.GetExtension(fullPath);
  54. if (extension == ".nca")
  55. {
  56. ResultCode result = FileSystemProxyHelper.OpenNcaFs(context, fullPath, fileStream.AsStorage(), out FileSystemProxy.IFileSystem fileSystem);
  57. if (result == ResultCode.Success)
  58. {
  59. MakeObject(context, fileSystem);
  60. }
  61. return result;
  62. }
  63. else if (extension == ".nsp")
  64. {
  65. ResultCode result = FileSystemProxyHelper.OpenNsp(context, fullPath, out FileSystemProxy.IFileSystem fileSystem);
  66. if (result == ResultCode.Success)
  67. {
  68. MakeObject(context, fileSystem);
  69. }
  70. return result;
  71. }
  72. return ResultCode.InvalidInput;
  73. }
  74. [Command(11)]
  75. // OpenBisFileSystem(nn::fssrv::sf::Partition partitionID, buffer<bytes<0x301>, 0x19, 0x301>) -> object<nn::fssrv::sf::IFileSystem> Bis
  76. public ResultCode OpenBisFileSystem(ServiceCtx context)
  77. {
  78. BisPartitionId bisPartitionId = (BisPartitionId)context.RequestData.ReadInt32();
  79. Result rc = FileSystemProxyHelper.ReadFsPath(out FsPath path, context);
  80. if (rc.IsFailure()) return (ResultCode)rc.Value;
  81. rc = _baseFileSystemProxy.OpenBisFileSystem(out LibHac.Fs.IFileSystem fileSystem, ref path, bisPartitionId);
  82. if (rc.IsFailure()) return (ResultCode)rc.Value;
  83. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  84. return ResultCode.Success;
  85. }
  86. [Command(18)]
  87. // OpenSdCardFileSystem() -> object<nn::fssrv::sf::IFileSystem>
  88. public ResultCode OpenSdCardFileSystem(ServiceCtx context)
  89. {
  90. Result rc = _baseFileSystemProxy.OpenSdCardFileSystem(out LibHac.Fs.IFileSystem fileSystem);
  91. if (rc.IsFailure()) return (ResultCode)rc.Value;
  92. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  93. return ResultCode.Success;
  94. }
  95. [Command(21)]
  96. public ResultCode DeleteSaveDataFileSystem(ServiceCtx context)
  97. {
  98. ulong saveDataId = context.RequestData.ReadUInt64();
  99. Result result = _baseFileSystemProxy.DeleteSaveDataFileSystem(saveDataId);
  100. return (ResultCode)result.Value;
  101. }
  102. [Command(22)]
  103. public ResultCode CreateSaveDataFileSystem(ServiceCtx context)
  104. {
  105. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  106. SaveDataCreationInfo creationInfo = context.RequestData.ReadStruct<SaveDataCreationInfo>();
  107. SaveMetaCreateInfo metaCreateInfo = context.RequestData.ReadStruct<SaveMetaCreateInfo>();
  108. // TODO: There's currently no program registry for FS to reference.
  109. // Workaround that by setting the application ID and owner ID if they're not already set
  110. if (attribute.TitleId == TitleId.Zero)
  111. {
  112. attribute.TitleId = new TitleId(context.Process.TitleId);
  113. }
  114. if (creationInfo.OwnerId == TitleId.Zero)
  115. {
  116. creationInfo.OwnerId = new TitleId(context.Process.TitleId);
  117. }
  118. Logger.PrintInfo(LogClass.ServiceFs, $"Creating save with title ID {attribute.TitleId.Value:x16}");
  119. Result result = _baseFileSystemProxy.CreateSaveDataFileSystem(ref attribute, ref creationInfo, ref metaCreateInfo);
  120. return (ResultCode)result.Value;
  121. }
  122. [Command(23)]
  123. public ResultCode CreateSaveDataFileSystemBySystemSaveDataId(ServiceCtx context)
  124. {
  125. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  126. SaveDataCreationInfo creationInfo = context.RequestData.ReadStruct<SaveDataCreationInfo>();
  127. Result result = _baseFileSystemProxy.CreateSaveDataFileSystemBySystemSaveDataId(ref attribute, ref creationInfo);
  128. return (ResultCode)result.Value;
  129. }
  130. [Command(25)]
  131. public ResultCode DeleteSaveDataFileSystemBySaveDataSpaceId(ServiceCtx context)
  132. {
  133. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  134. ulong saveDataId = context.RequestData.ReadUInt64();
  135. Result result = _baseFileSystemProxy.DeleteSaveDataFileSystemBySaveDataSpaceId(spaceId, saveDataId);
  136. return (ResultCode)result.Value;
  137. }
  138. [Command(28)]
  139. public ResultCode DeleteSaveDataFileSystemBySaveDataAttribute(ServiceCtx context)
  140. {
  141. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  142. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  143. Result result = _baseFileSystemProxy.DeleteSaveDataFileSystemBySaveDataAttribute(spaceId, ref attribute);
  144. return (ResultCode)result.Value;
  145. }
  146. [Command(30)]
  147. // OpenGameCardStorage(u32, u32) -> object<nn::fssrv::sf::IStorage>
  148. public ResultCode OpenGameCardStorage(ServiceCtx context)
  149. {
  150. GameCardHandle handle = new GameCardHandle(context.RequestData.ReadInt32());
  151. GameCardPartitionRaw partitionId = (GameCardPartitionRaw)context.RequestData.ReadInt32();
  152. Result result = _baseFileSystemProxy.OpenGameCardStorage(out LibHac.Fs.IStorage storage, handle, partitionId);
  153. if (result.IsSuccess())
  154. {
  155. MakeObject(context, new FileSystemProxy.IStorage(storage));
  156. }
  157. return (ResultCode)result.Value;
  158. }
  159. [Command(35)]
  160. public ResultCode CreateSaveDataFileSystemWithHashSalt(ServiceCtx context)
  161. {
  162. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  163. SaveDataCreationInfo creationInfo = context.RequestData.ReadStruct<SaveDataCreationInfo>();
  164. SaveMetaCreateInfo metaCreateInfo = context.RequestData.ReadStruct<SaveMetaCreateInfo>();
  165. HashSalt hashSalt = context.RequestData.ReadStruct<HashSalt>();
  166. // TODO: There's currently no program registry for FS to reference.
  167. // Workaround that by setting the application ID and owner ID if they're not already set
  168. if (attribute.TitleId == TitleId.Zero)
  169. {
  170. attribute.TitleId = new TitleId(context.Process.TitleId);
  171. }
  172. if (creationInfo.OwnerId == TitleId.Zero)
  173. {
  174. creationInfo.OwnerId = new TitleId(context.Process.TitleId);
  175. }
  176. Result result = _baseFileSystemProxy.CreateSaveDataFileSystemWithHashSalt(ref attribute, ref creationInfo, ref metaCreateInfo, ref hashSalt);
  177. return (ResultCode)result.Value;
  178. }
  179. [Command(51)]
  180. // OpenSaveDataFileSystem(u8 save_data_space_id, nn::fssrv::sf::SaveStruct saveStruct) -> object<nn::fssrv::sf::IFileSystem> saveDataFs
  181. public ResultCode OpenSaveDataFileSystem(ServiceCtx context)
  182. {
  183. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  184. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  185. // TODO: There's currently no program registry for FS to reference.
  186. // Workaround that by setting the application ID if it's not already set
  187. if (attribute.TitleId == TitleId.Zero)
  188. {
  189. attribute.TitleId = new TitleId(context.Process.TitleId);
  190. }
  191. Result result = _baseFileSystemProxy.OpenSaveDataFileSystem(out LibHac.Fs.IFileSystem fileSystem, spaceId, ref attribute);
  192. if (result.IsSuccess())
  193. {
  194. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  195. }
  196. return (ResultCode)result.Value;
  197. }
  198. [Command(52)]
  199. // OpenSaveDataFileSystemBySystemSaveDataId(u8 save_data_space_id, nn::fssrv::sf::SaveStruct saveStruct) -> object<nn::fssrv::sf::IFileSystem> systemSaveDataFs
  200. public ResultCode OpenSaveDataFileSystemBySystemSaveDataId(ServiceCtx context)
  201. {
  202. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  203. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  204. Result result = _baseFileSystemProxy.OpenSaveDataFileSystemBySystemSaveDataId(out LibHac.Fs.IFileSystem fileSystem, spaceId, ref attribute);
  205. if (result.IsSuccess())
  206. {
  207. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  208. }
  209. return (ResultCode)result.Value;
  210. }
  211. [Command(53)]
  212. // OpenReadOnlySaveDataFileSystem(u8 save_data_space_id, nn::fssrv::sf::SaveStruct save_struct) -> object<nn::fssrv::sf::IFileSystem>
  213. public ResultCode OpenReadOnlySaveDataFileSystem(ServiceCtx context)
  214. {
  215. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  216. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  217. // TODO: There's currently no program registry for FS to reference.
  218. // Workaround that by setting the application ID if it's not already set
  219. if (attribute.TitleId == TitleId.Zero)
  220. {
  221. attribute.TitleId = new TitleId(context.Process.TitleId);
  222. }
  223. Result result = _baseFileSystemProxy.OpenReadOnlySaveDataFileSystem(out LibHac.Fs.IFileSystem fileSystem, spaceId, ref attribute);
  224. if (result.IsSuccess())
  225. {
  226. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  227. }
  228. return (ResultCode)result.Value;
  229. }
  230. [Command(60)]
  231. public ResultCode OpenSaveDataInfoReader(ServiceCtx context)
  232. {
  233. Result result = _baseFileSystemProxy.OpenSaveDataInfoReader(out LibHac.FsService.ISaveDataInfoReader infoReader);
  234. if (result.IsSuccess())
  235. {
  236. MakeObject(context, new ISaveDataInfoReader(infoReader));
  237. }
  238. return (ResultCode)result.Value;
  239. }
  240. [Command(61)]
  241. public ResultCode OpenSaveDataInfoReaderBySaveDataSpaceId(ServiceCtx context)
  242. {
  243. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadByte();
  244. Result result = _baseFileSystemProxy.OpenSaveDataInfoReaderBySaveDataSpaceId(out LibHac.FsService.ISaveDataInfoReader infoReader, spaceId);
  245. if (result.IsSuccess())
  246. {
  247. MakeObject(context, new ISaveDataInfoReader(infoReader));
  248. }
  249. return (ResultCode)result.Value;
  250. }
  251. [Command(67)]
  252. public ResultCode FindSaveDataWithFilter(ServiceCtx context)
  253. {
  254. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  255. SaveDataFilter filter = context.RequestData.ReadStruct<SaveDataFilter>();
  256. long bufferPosition = context.Request.ReceiveBuff[0].Position;
  257. long bufferLen = context.Request.ReceiveBuff[0].Size;
  258. byte[] infoBuffer = new byte[bufferLen];
  259. Result result = _baseFileSystemProxy.FindSaveDataWithFilter(out long count, infoBuffer, spaceId, ref filter);
  260. context.Memory.Write((ulong)bufferPosition, infoBuffer);
  261. context.ResponseData.Write(count);
  262. return (ResultCode)result.Value;
  263. }
  264. [Command(68)]
  265. public ResultCode OpenSaveDataInfoReaderWithFilter(ServiceCtx context)
  266. {
  267. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  268. SaveDataFilter filter = context.RequestData.ReadStruct<SaveDataFilter>();
  269. Result result = _baseFileSystemProxy.OpenSaveDataInfoReaderWithFilter(out LibHac.FsService.ISaveDataInfoReader infoReader, spaceId, ref filter);
  270. if (result.IsSuccess())
  271. {
  272. MakeObject(context, new ISaveDataInfoReader(infoReader));
  273. }
  274. return (ResultCode)result.Value;
  275. }
  276. [Command(71)]
  277. public ResultCode ReadSaveDataFileSystemExtraDataWithMaskBySaveDataAttribute(ServiceCtx context)
  278. {
  279. Logger.PrintStub(LogClass.ServiceFs);
  280. MemoryHelper.FillWithZeros(context.Memory, context.Request.ReceiveBuff[0].Position, (int)context.Request.ReceiveBuff[0].Size);
  281. return ResultCode.Success;
  282. }
  283. [Command(200)]
  284. // OpenDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage> dataStorage
  285. public ResultCode OpenDataStorageByCurrentProcess(ServiceCtx context)
  286. {
  287. MakeObject(context, new FileSystemProxy.IStorage(context.Device.FileSystem.RomFs.AsStorage()));
  288. return 0;
  289. }
  290. [Command(202)]
  291. // OpenDataStorageByDataId(u8 storageId, nn::ApplicationId tid) -> object<nn::fssrv::sf::IStorage> dataStorage
  292. public ResultCode OpenDataStorageByDataId(ServiceCtx context)
  293. {
  294. StorageId storageId = (StorageId)context.RequestData.ReadByte();
  295. byte[] padding = context.RequestData.ReadBytes(7);
  296. long titleId = context.RequestData.ReadInt64();
  297. // We do a mitm here to find if the request is for an AOC.
  298. // This is because AOC can be distributed over multiple containers in the emulator.
  299. if (context.Device.System.ContentManager.GetAocDataStorage((ulong)titleId, out LibHac.Fs.IStorage aocStorage))
  300. {
  301. Logger.PrintInfo(LogClass.Loader, $"Opened AddOnContent Data TitleID={titleId:X16}");
  302. MakeObject(context, new FileSystemProxy.IStorage(aocStorage));
  303. return ResultCode.Success;
  304. }
  305. NcaContentType contentType = NcaContentType.Data;
  306. StorageId installedStorage = context.Device.System.ContentManager.GetInstalledStorage(titleId, contentType, storageId);
  307. if (installedStorage == StorageId.None)
  308. {
  309. contentType = NcaContentType.PublicData;
  310. installedStorage = context.Device.System.ContentManager.GetInstalledStorage(titleId, contentType, storageId);
  311. }
  312. if (installedStorage != StorageId.None)
  313. {
  314. string contentPath = context.Device.System.ContentManager.GetInstalledContentPath(titleId, storageId, contentType);
  315. string installPath = context.Device.FileSystem.SwitchPathToSystemPath(contentPath);
  316. if (!string.IsNullOrWhiteSpace(installPath))
  317. {
  318. string ncaPath = installPath;
  319. if (File.Exists(ncaPath))
  320. {
  321. try
  322. {
  323. LibHac.Fs.IStorage ncaStorage = new LocalStorage(ncaPath, FileAccess.Read, FileMode.Open);
  324. Nca nca = new Nca(context.Device.System.KeySet, ncaStorage);
  325. LibHac.Fs.IStorage romfsStorage = nca.OpenStorage(NcaSectionType.Data, context.Device.System.FsIntegrityCheckLevel);
  326. MakeObject(context, new FileSystemProxy.IStorage(romfsStorage));
  327. }
  328. catch (HorizonResultException ex)
  329. {
  330. return (ResultCode)ex.ResultValue.Value;
  331. }
  332. return ResultCode.Success;
  333. }
  334. else
  335. {
  336. throw new FileNotFoundException($"No Nca found in Path `{ncaPath}`.");
  337. }
  338. }
  339. else
  340. {
  341. throw new DirectoryNotFoundException($"Path for title id {titleId:x16} on Storage {storageId} was not found in Path {installPath}.");
  342. }
  343. }
  344. throw new FileNotFoundException($"System archive with titleid {titleId:x16} was not found on Storage {storageId}. Found in {installedStorage}.");
  345. }
  346. [Command(203)]
  347. // OpenPatchDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage>
  348. public ResultCode OpenPatchDataStorageByCurrentProcess(ServiceCtx context)
  349. {
  350. MakeObject(context, new FileSystemProxy.IStorage(context.Device.FileSystem.RomFs.AsStorage()));
  351. return ResultCode.Success;
  352. }
  353. [Command(400)]
  354. // OpenDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage> dataStorage
  355. public ResultCode OpenDeviceOperator(ServiceCtx context)
  356. {
  357. Result result = _baseFileSystemProxy.OpenDeviceOperator(out LibHac.FsService.IDeviceOperator deviceOperator);
  358. if (result.IsSuccess())
  359. {
  360. MakeObject(context, new IDeviceOperator(deviceOperator));
  361. }
  362. return (ResultCode)result.Value;
  363. }
  364. [Command(630)]
  365. // SetSdCardAccessibility(u8)
  366. public ResultCode SetSdCardAccessibility(ServiceCtx context)
  367. {
  368. bool isAccessible = context.RequestData.ReadBoolean();
  369. return (ResultCode)_baseFileSystemProxy.SetSdCardAccessibility(isAccessible).Value;
  370. }
  371. [Command(631)]
  372. // IsSdCardAccessible() -> u8
  373. public ResultCode IsSdCardAccessible(ServiceCtx context)
  374. {
  375. Result result = _baseFileSystemProxy.IsSdCardAccessible(out bool isAccessible);
  376. context.ResponseData.Write(isAccessible);
  377. return (ResultCode)result.Value;
  378. }
  379. [Command(1004)]
  380. // SetGlobalAccessLogMode(u32 mode)
  381. public ResultCode SetGlobalAccessLogMode(ServiceCtx context)
  382. {
  383. int mode = context.RequestData.ReadInt32();
  384. context.Device.System.GlobalAccessLogMode = mode;
  385. return ResultCode.Success;
  386. }
  387. [Command(1005)]
  388. // GetGlobalAccessLogMode() -> u32 logMode
  389. public ResultCode GetGlobalAccessLogMode(ServiceCtx context)
  390. {
  391. int mode = context.Device.System.GlobalAccessLogMode;
  392. context.ResponseData.Write(mode);
  393. return ResultCode.Success;
  394. }
  395. [Command(1006)]
  396. // OutputAccessLogToSdCard(buffer<bytes, 5> log_text)
  397. public ResultCode OutputAccessLogToSdCard(ServiceCtx context)
  398. {
  399. string message = ReadUtf8StringSend(context);
  400. // FS ends each line with a newline. Remove it because Ryujinx logging adds its own newline
  401. Logger.PrintAccessLog(LogClass.ServiceFs, message.TrimEnd('\n'));
  402. return ResultCode.Success;
  403. }
  404. [Command(1011)]
  405. public ResultCode GetProgramIndexForAccessLog(ServiceCtx context)
  406. {
  407. int programIndex = 0;
  408. int programCount = 1;
  409. context.ResponseData.Write(programIndex);
  410. context.ResponseData.Write(programCount);
  411. return ResultCode.Success;
  412. }
  413. [Command(1200)] // 6.0.0+
  414. // OpenMultiCommitManager() -> object<nn::fssrv::sf::IMultiCommitManager>
  415. public ResultCode OpenMultiCommitManager(ServiceCtx context)
  416. {
  417. Result result = _baseFileSystemProxy.OpenMultiCommitManager(out LibHac.FsService.IMultiCommitManager commitManager);
  418. if (result.IsSuccess())
  419. {
  420. MakeObject(context, new IMultiCommitManager(commitManager));
  421. }
  422. return (ResultCode)result.Value;
  423. }
  424. }
  425. }