IFileSystemProxy.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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.HLE.HOS.Services.Fs.FileSystemProxy;
  10. using System.IO;
  11. using static Ryujinx.HLE.Utilities.StringUtils;
  12. using StorageId = Ryujinx.HLE.FileSystem.StorageId;
  13. namespace Ryujinx.HLE.HOS.Services.Fs
  14. {
  15. [Service("fsp-srv")]
  16. class IFileSystemProxy : IpcService
  17. {
  18. private LibHac.FsService.IFileSystemProxy _baseFileSystemProxy;
  19. public IFileSystemProxy(ServiceCtx context)
  20. {
  21. _baseFileSystemProxy = context.Device.System.FsServer.CreateFileSystemProxyService();
  22. }
  23. [Command(1)]
  24. // Initialize(u64, pid)
  25. public ResultCode Initialize(ServiceCtx context)
  26. {
  27. return ResultCode.Success;
  28. }
  29. [Command(8)]
  30. // OpenFileSystemWithId(nn::fssrv::sf::FileSystemType filesystem_type, nn::ApplicationId tid, buffer<bytes<0x301>, 0x19, 0x301> path)
  31. // -> object<nn::fssrv::sf::IFileSystem> contentFs
  32. public ResultCode OpenFileSystemWithId(ServiceCtx context)
  33. {
  34. FileSystemType fileSystemType = (FileSystemType)context.RequestData.ReadInt32();
  35. long titleId = context.RequestData.ReadInt64();
  36. string switchPath = ReadUtf8String(context);
  37. string fullPath = context.Device.FileSystem.SwitchPathToSystemPath(switchPath);
  38. if (!File.Exists(fullPath))
  39. {
  40. if (fullPath.Contains("."))
  41. {
  42. ResultCode result = FileSystemProxyHelper.OpenFileSystemFromInternalFile(context, fullPath, out FileSystemProxy.IFileSystem fileSystem);
  43. if (result == ResultCode.Success)
  44. {
  45. MakeObject(context, fileSystem);
  46. }
  47. return result;
  48. }
  49. return ResultCode.PathDoesNotExist;
  50. }
  51. FileStream fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
  52. string extension = Path.GetExtension(fullPath);
  53. if (extension == ".nca")
  54. {
  55. ResultCode result = FileSystemProxyHelper.OpenNcaFs(context, fullPath, fileStream.AsStorage(), out FileSystemProxy.IFileSystem fileSystem);
  56. if (result == ResultCode.Success)
  57. {
  58. MakeObject(context, fileSystem);
  59. }
  60. return result;
  61. }
  62. else if (extension == ".nsp")
  63. {
  64. ResultCode result = FileSystemProxyHelper.OpenNsp(context, fullPath, out FileSystemProxy.IFileSystem fileSystem);
  65. if (result == ResultCode.Success)
  66. {
  67. MakeObject(context, fileSystem);
  68. }
  69. return result;
  70. }
  71. return ResultCode.InvalidInput;
  72. }
  73. [Command(11)]
  74. // OpenBisFileSystem(nn::fssrv::sf::Partition partitionID, buffer<bytes<0x301>, 0x19, 0x301>) -> object<nn::fssrv::sf::IFileSystem> Bis
  75. public ResultCode OpenBisFileSystem(ServiceCtx context)
  76. {
  77. BisPartitionId bisPartitionId = (BisPartitionId)context.RequestData.ReadInt32();
  78. Result rc = FileSystemProxyHelper.ReadFsPath(out FsPath path, context);
  79. if (rc.IsFailure()) return (ResultCode)rc.Value;
  80. rc = _baseFileSystemProxy.OpenBisFileSystem(out LibHac.Fs.IFileSystem fileSystem, ref path, bisPartitionId);
  81. if (rc.IsFailure()) return (ResultCode)rc.Value;
  82. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  83. return ResultCode.Success;
  84. }
  85. [Command(18)]
  86. // OpenSdCardFileSystem() -> object<nn::fssrv::sf::IFileSystem>
  87. public ResultCode OpenSdCardFileSystem(ServiceCtx context)
  88. {
  89. Result rc = _baseFileSystemProxy.OpenSdCardFileSystem(out LibHac.Fs.IFileSystem fileSystem);
  90. if (rc.IsFailure()) return (ResultCode)rc.Value;
  91. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  92. return ResultCode.Success;
  93. }
  94. [Command(21)]
  95. public ResultCode DeleteSaveDataFileSystem(ServiceCtx context)
  96. {
  97. ulong saveDataId = context.RequestData.ReadUInt64();
  98. Result result = _baseFileSystemProxy.DeleteSaveDataFileSystem(saveDataId);
  99. return (ResultCode)result.Value;
  100. }
  101. [Command(22)]
  102. public ResultCode CreateSaveDataFileSystem(ServiceCtx context)
  103. {
  104. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  105. SaveDataCreateInfo createInfo = context.RequestData.ReadStruct<SaveDataCreateInfo>();
  106. SaveMetaCreateInfo metaCreateInfo = context.RequestData.ReadStruct<SaveMetaCreateInfo>();
  107. Result result = _baseFileSystemProxy.CreateSaveDataFileSystem(ref attribute, ref createInfo, ref metaCreateInfo);
  108. return (ResultCode)result.Value;
  109. }
  110. [Command(23)]
  111. public ResultCode CreateSaveDataFileSystemBySystemSaveDataId(ServiceCtx context)
  112. {
  113. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  114. SaveDataCreateInfo createInfo = context.RequestData.ReadStruct<SaveDataCreateInfo>();
  115. Result result = _baseFileSystemProxy.CreateSaveDataFileSystemBySystemSaveDataId(ref attribute, ref createInfo);
  116. return (ResultCode)result.Value;
  117. }
  118. [Command(25)]
  119. public ResultCode DeleteSaveDataFileSystemBySaveDataSpaceId(ServiceCtx context)
  120. {
  121. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  122. ulong saveDataId = context.RequestData.ReadUInt64();
  123. Result result = _baseFileSystemProxy.DeleteSaveDataFileSystemBySaveDataSpaceId(spaceId, saveDataId);
  124. return (ResultCode)result.Value;
  125. }
  126. [Command(28)]
  127. public ResultCode DeleteSaveDataFileSystemBySaveDataAttribute(ServiceCtx context)
  128. {
  129. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  130. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  131. Result result = _baseFileSystemProxy.DeleteSaveDataFileSystemBySaveDataAttribute(spaceId, ref attribute);
  132. return (ResultCode)result.Value;
  133. }
  134. [Command(30)]
  135. // OpenGameCardStorage(u32, u32) -> object<nn::fssrv::sf::IStorage>
  136. public ResultCode OpenGameCardStorage(ServiceCtx context)
  137. {
  138. GameCardHandle handle = new GameCardHandle(context.RequestData.ReadInt32());
  139. GameCardPartitionRaw partitionId = (GameCardPartitionRaw)context.RequestData.ReadInt32();
  140. Result result = _baseFileSystemProxy.OpenGameCardStorage(out LibHac.Fs.IStorage storage, handle, partitionId);
  141. if (result.IsSuccess())
  142. {
  143. MakeObject(context, new FileSystemProxy.IStorage(storage));
  144. }
  145. return (ResultCode)result.Value;
  146. }
  147. [Command(35)]
  148. public ResultCode CreateSaveDataFileSystemWithHashSalt(ServiceCtx context)
  149. {
  150. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  151. SaveDataCreateInfo createInfo = context.RequestData.ReadStruct<SaveDataCreateInfo>();
  152. SaveMetaCreateInfo metaCreateInfo = context.RequestData.ReadStruct<SaveMetaCreateInfo>();
  153. HashSalt hashSalt = context.RequestData.ReadStruct<HashSalt>();
  154. Result result = _baseFileSystemProxy.CreateSaveDataFileSystemWithHashSalt(ref attribute, ref createInfo, ref metaCreateInfo, ref hashSalt);
  155. return (ResultCode)result.Value;
  156. }
  157. [Command(51)]
  158. // OpenSaveDataFileSystem(u8 save_data_space_id, nn::fssrv::sf::SaveStruct saveStruct) -> object<nn::fssrv::sf::IFileSystem> saveDataFs
  159. public ResultCode OpenSaveDataFileSystem(ServiceCtx context)
  160. {
  161. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  162. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  163. if (attribute.TitleId == TitleId.Zero)
  164. {
  165. attribute.TitleId = new TitleId(context.Process.TitleId);
  166. }
  167. Result result = _baseFileSystemProxy.OpenSaveDataFileSystem(out LibHac.Fs.IFileSystem fileSystem, spaceId, ref attribute);
  168. if (result.IsSuccess())
  169. {
  170. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  171. }
  172. return (ResultCode)result.Value;
  173. }
  174. [Command(52)]
  175. // OpenSaveDataFileSystemBySystemSaveDataId(u8 save_data_space_id, nn::fssrv::sf::SaveStruct saveStruct) -> object<nn::fssrv::sf::IFileSystem> systemSaveDataFs
  176. public ResultCode OpenSaveDataFileSystemBySystemSaveDataId(ServiceCtx context)
  177. {
  178. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  179. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  180. Result result = _baseFileSystemProxy.OpenSaveDataFileSystemBySystemSaveDataId(out LibHac.Fs.IFileSystem fileSystem, spaceId, ref attribute);
  181. if (result.IsSuccess())
  182. {
  183. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  184. }
  185. return (ResultCode)result.Value;
  186. }
  187. [Command(53)]
  188. // OpenReadOnlySaveDataFileSystem(u8 save_data_space_id, nn::fssrv::sf::SaveStruct save_struct) -> object<nn::fssrv::sf::IFileSystem>
  189. public ResultCode OpenReadOnlySaveDataFileSystem(ServiceCtx context)
  190. {
  191. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  192. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  193. if (attribute.TitleId == TitleId.Zero)
  194. {
  195. attribute.TitleId = new TitleId(context.Process.TitleId);
  196. }
  197. Result result = _baseFileSystemProxy.OpenReadOnlySaveDataFileSystem(out LibHac.Fs.IFileSystem fileSystem, spaceId, ref attribute);
  198. if (result.IsSuccess())
  199. {
  200. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  201. }
  202. return (ResultCode)result.Value;
  203. }
  204. [Command(60)]
  205. public ResultCode OpenSaveDataInfoReader(ServiceCtx context)
  206. {
  207. Result result = _baseFileSystemProxy.OpenSaveDataInfoReader(out LibHac.FsService.ISaveDataInfoReader infoReader);
  208. if (result.IsSuccess())
  209. {
  210. MakeObject(context, new ISaveDataInfoReader(infoReader));
  211. }
  212. return (ResultCode)result.Value;
  213. }
  214. [Command(61)]
  215. public ResultCode OpenSaveDataInfoReaderBySaveDataSpaceId(ServiceCtx context)
  216. {
  217. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadByte();
  218. Result result = _baseFileSystemProxy.OpenSaveDataInfoReaderBySaveDataSpaceId(out LibHac.FsService.ISaveDataInfoReader infoReader, spaceId);
  219. if (result.IsSuccess())
  220. {
  221. MakeObject(context, new ISaveDataInfoReader(infoReader));
  222. }
  223. return (ResultCode)result.Value;
  224. }
  225. [Command(67)]
  226. public ResultCode FindSaveDataWithFilter(ServiceCtx context)
  227. {
  228. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  229. SaveDataFilter filter = context.RequestData.ReadStruct<SaveDataFilter>();
  230. long bufferPosition = context.Request.ReceiveBuff[0].Position;
  231. long bufferLen = context.Request.ReceiveBuff[0].Size;
  232. byte[] infoBuffer = new byte[bufferLen];
  233. Result result = _baseFileSystemProxy.FindSaveDataWithFilter(out long count, infoBuffer, spaceId, ref filter);
  234. context.Memory.WriteBytes(bufferPosition, infoBuffer);
  235. context.ResponseData.Write(count);
  236. return (ResultCode)result.Value;
  237. }
  238. [Command(68)]
  239. public ResultCode OpenSaveDataInfoReaderWithFilter(ServiceCtx context)
  240. {
  241. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  242. SaveDataFilter filter = context.RequestData.ReadStruct<SaveDataFilter>();
  243. Result result = _baseFileSystemProxy.OpenSaveDataInfoReaderWithFilter(out LibHac.FsService.ISaveDataInfoReader infoReader, spaceId, ref filter);
  244. if (result.IsSuccess())
  245. {
  246. MakeObject(context, new ISaveDataInfoReader(infoReader));
  247. }
  248. return (ResultCode)result.Value;
  249. }
  250. [Command(200)]
  251. // OpenDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage> dataStorage
  252. public ResultCode OpenDataStorageByCurrentProcess(ServiceCtx context)
  253. {
  254. MakeObject(context, new FileSystemProxy.IStorage(context.Device.FileSystem.RomFs.AsStorage()));
  255. return 0;
  256. }
  257. [Command(202)]
  258. // OpenDataStorageByDataId(u8 storageId, nn::ApplicationId tid) -> object<nn::fssrv::sf::IStorage> dataStorage
  259. public ResultCode OpenDataStorageByDataId(ServiceCtx context)
  260. {
  261. StorageId storageId = (StorageId)context.RequestData.ReadByte();
  262. byte[] padding = context.RequestData.ReadBytes(7);
  263. long titleId = context.RequestData.ReadInt64();
  264. NcaContentType contentType = NcaContentType.Data;
  265. StorageId installedStorage =
  266. context.Device.System.ContentManager.GetInstalledStorage(titleId, contentType, storageId);
  267. if (installedStorage == StorageId.None)
  268. {
  269. contentType = NcaContentType.PublicData;
  270. installedStorage =
  271. context.Device.System.ContentManager.GetInstalledStorage(titleId, contentType, storageId);
  272. }
  273. if (installedStorage != StorageId.None)
  274. {
  275. string contentPath = context.Device.System.ContentManager.GetInstalledContentPath(titleId, storageId, contentType);
  276. string installPath = context.Device.FileSystem.SwitchPathToSystemPath(contentPath);
  277. if (!string.IsNullOrWhiteSpace(installPath))
  278. {
  279. string ncaPath = installPath;
  280. if (File.Exists(ncaPath))
  281. {
  282. try
  283. {
  284. LibHac.Fs.IStorage ncaStorage = new LocalStorage(ncaPath, FileAccess.Read, FileMode.Open);
  285. Nca nca = new Nca(context.Device.System.KeySet, ncaStorage);
  286. LibHac.Fs.IStorage romfsStorage = nca.OpenStorage(NcaSectionType.Data, context.Device.System.FsIntegrityCheckLevel);
  287. MakeObject(context, new FileSystemProxy.IStorage(romfsStorage));
  288. }
  289. catch (HorizonResultException ex)
  290. {
  291. return (ResultCode)ex.ResultValue.Value;
  292. }
  293. return ResultCode.Success;
  294. }
  295. else
  296. {
  297. throw new FileNotFoundException($"No Nca found in Path `{ncaPath}`.");
  298. }
  299. }
  300. else
  301. {
  302. throw new DirectoryNotFoundException($"Path for title id {titleId:x16} on Storage {storageId} was not found in Path {installPath}.");
  303. }
  304. }
  305. throw new FileNotFoundException($"System archive with titleid {titleId:x16} was not found on Storage {storageId}. Found in {installedStorage}.");
  306. }
  307. [Command(203)]
  308. // OpenPatchDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage>
  309. public ResultCode OpenPatchDataStorageByCurrentProcess(ServiceCtx context)
  310. {
  311. MakeObject(context, new FileSystemProxy.IStorage(context.Device.FileSystem.RomFs.AsStorage()));
  312. return ResultCode.Success;
  313. }
  314. [Command(400)]
  315. // OpenDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage> dataStorage
  316. public ResultCode OpenDeviceOperator(ServiceCtx context)
  317. {
  318. Result result = _baseFileSystemProxy.OpenDeviceOperator(out LibHac.FsService.IDeviceOperator deviceOperator);
  319. if (result.IsSuccess())
  320. {
  321. MakeObject(context, new IDeviceOperator(deviceOperator));
  322. }
  323. return (ResultCode)result.Value;
  324. }
  325. [Command(1005)]
  326. // GetGlobalAccessLogMode() -> u32 logMode
  327. public ResultCode GetGlobalAccessLogMode(ServiceCtx context)
  328. {
  329. int mode = context.Device.System.GlobalAccessLogMode;
  330. context.ResponseData.Write(mode);
  331. return ResultCode.Success;
  332. }
  333. [Command(1006)]
  334. // OutputAccessLogToSdCard(buffer<bytes, 5> log_text)
  335. public ResultCode OutputAccessLogToSdCard(ServiceCtx context)
  336. {
  337. string message = ReadUtf8StringSend(context);
  338. // FS ends each line with a newline. Remove it because Ryujinx logging adds its own newline
  339. Logger.PrintAccessLog(LogClass.ServiceFs, message.TrimEnd('\n'));
  340. return ResultCode.Success;
  341. }
  342. [Command(1011)]
  343. public ResultCode GetProgramIndexForAccessLog(ServiceCtx context)
  344. {
  345. int programIndex = 0;
  346. int programCount = 1;
  347. context.ResponseData.Write(programIndex);
  348. context.ResponseData.Write(programCount);
  349. return ResultCode.Success;
  350. }
  351. }
  352. }