IFileSystemProxy.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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.FileSystem.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. SaveDataCreationInfo creationInfo = context.RequestData.ReadStruct<SaveDataCreationInfo>();
  106. SaveMetaCreateInfo metaCreateInfo = context.RequestData.ReadStruct<SaveMetaCreateInfo>();
  107. // TODO: There's currently no program registry for FS to reference.
  108. // Workaround that by setting the application ID and owner ID if they're not already set
  109. if (attribute.TitleId == TitleId.Zero)
  110. {
  111. attribute.TitleId = new TitleId(context.Process.TitleId);
  112. }
  113. if (creationInfo.OwnerId == TitleId.Zero)
  114. {
  115. creationInfo.OwnerId = new TitleId(context.Process.TitleId);
  116. }
  117. Logger.PrintInfo(LogClass.ServiceFs, $"Creating save with title ID {attribute.TitleId.Value:x16}");
  118. Result result = _baseFileSystemProxy.CreateSaveDataFileSystem(ref attribute, ref creationInfo, ref metaCreateInfo);
  119. return (ResultCode)result.Value;
  120. }
  121. [Command(23)]
  122. public ResultCode CreateSaveDataFileSystemBySystemSaveDataId(ServiceCtx context)
  123. {
  124. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  125. SaveDataCreationInfo creationInfo = context.RequestData.ReadStruct<SaveDataCreationInfo>();
  126. Result result = _baseFileSystemProxy.CreateSaveDataFileSystemBySystemSaveDataId(ref attribute, ref creationInfo);
  127. return (ResultCode)result.Value;
  128. }
  129. [Command(25)]
  130. public ResultCode DeleteSaveDataFileSystemBySaveDataSpaceId(ServiceCtx context)
  131. {
  132. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  133. ulong saveDataId = context.RequestData.ReadUInt64();
  134. Result result = _baseFileSystemProxy.DeleteSaveDataFileSystemBySaveDataSpaceId(spaceId, saveDataId);
  135. return (ResultCode)result.Value;
  136. }
  137. [Command(28)]
  138. public ResultCode DeleteSaveDataFileSystemBySaveDataAttribute(ServiceCtx context)
  139. {
  140. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  141. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  142. Result result = _baseFileSystemProxy.DeleteSaveDataFileSystemBySaveDataAttribute(spaceId, ref attribute);
  143. return (ResultCode)result.Value;
  144. }
  145. [Command(30)]
  146. // OpenGameCardStorage(u32, u32) -> object<nn::fssrv::sf::IStorage>
  147. public ResultCode OpenGameCardStorage(ServiceCtx context)
  148. {
  149. GameCardHandle handle = new GameCardHandle(context.RequestData.ReadInt32());
  150. GameCardPartitionRaw partitionId = (GameCardPartitionRaw)context.RequestData.ReadInt32();
  151. Result result = _baseFileSystemProxy.OpenGameCardStorage(out LibHac.Fs.IStorage storage, handle, partitionId);
  152. if (result.IsSuccess())
  153. {
  154. MakeObject(context, new FileSystemProxy.IStorage(storage));
  155. }
  156. return (ResultCode)result.Value;
  157. }
  158. [Command(35)]
  159. public ResultCode CreateSaveDataFileSystemWithHashSalt(ServiceCtx context)
  160. {
  161. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  162. SaveDataCreationInfo creationInfo = context.RequestData.ReadStruct<SaveDataCreationInfo>();
  163. SaveMetaCreateInfo metaCreateInfo = context.RequestData.ReadStruct<SaveMetaCreateInfo>();
  164. HashSalt hashSalt = context.RequestData.ReadStruct<HashSalt>();
  165. // TODO: There's currently no program registry for FS to reference.
  166. // Workaround that by setting the application ID and owner ID if they're not already set
  167. if (attribute.TitleId == TitleId.Zero)
  168. {
  169. attribute.TitleId = new TitleId(context.Process.TitleId);
  170. }
  171. if (creationInfo.OwnerId == TitleId.Zero)
  172. {
  173. creationInfo.OwnerId = new TitleId(context.Process.TitleId);
  174. }
  175. Result result = _baseFileSystemProxy.CreateSaveDataFileSystemWithHashSalt(ref attribute, ref creationInfo, ref metaCreateInfo, ref hashSalt);
  176. return (ResultCode)result.Value;
  177. }
  178. [Command(51)]
  179. // OpenSaveDataFileSystem(u8 save_data_space_id, nn::fssrv::sf::SaveStruct saveStruct) -> object<nn::fssrv::sf::IFileSystem> saveDataFs
  180. public ResultCode OpenSaveDataFileSystem(ServiceCtx context)
  181. {
  182. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  183. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  184. // TODO: There's currently no program registry for FS to reference.
  185. // Workaround that by setting the application ID if it's not already set
  186. if (attribute.TitleId == TitleId.Zero)
  187. {
  188. attribute.TitleId = new TitleId(context.Process.TitleId);
  189. }
  190. Result result = _baseFileSystemProxy.OpenSaveDataFileSystem(out LibHac.Fs.IFileSystem fileSystem, spaceId, ref attribute);
  191. if (result.IsSuccess())
  192. {
  193. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  194. }
  195. return (ResultCode)result.Value;
  196. }
  197. [Command(52)]
  198. // OpenSaveDataFileSystemBySystemSaveDataId(u8 save_data_space_id, nn::fssrv::sf::SaveStruct saveStruct) -> object<nn::fssrv::sf::IFileSystem> systemSaveDataFs
  199. public ResultCode OpenSaveDataFileSystemBySystemSaveDataId(ServiceCtx context)
  200. {
  201. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  202. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  203. Result result = _baseFileSystemProxy.OpenSaveDataFileSystemBySystemSaveDataId(out LibHac.Fs.IFileSystem fileSystem, spaceId, ref attribute);
  204. if (result.IsSuccess())
  205. {
  206. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  207. }
  208. return (ResultCode)result.Value;
  209. }
  210. [Command(53)]
  211. // OpenReadOnlySaveDataFileSystem(u8 save_data_space_id, nn::fssrv::sf::SaveStruct save_struct) -> object<nn::fssrv::sf::IFileSystem>
  212. public ResultCode OpenReadOnlySaveDataFileSystem(ServiceCtx context)
  213. {
  214. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  215. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  216. // TODO: There's currently no program registry for FS to reference.
  217. // Workaround that by setting the application ID if it's not already set
  218. if (attribute.TitleId == TitleId.Zero)
  219. {
  220. attribute.TitleId = new TitleId(context.Process.TitleId);
  221. }
  222. Result result = _baseFileSystemProxy.OpenReadOnlySaveDataFileSystem(out LibHac.Fs.IFileSystem fileSystem, spaceId, ref attribute);
  223. if (result.IsSuccess())
  224. {
  225. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  226. }
  227. return (ResultCode)result.Value;
  228. }
  229. [Command(60)]
  230. public ResultCode OpenSaveDataInfoReader(ServiceCtx context)
  231. {
  232. Result result = _baseFileSystemProxy.OpenSaveDataInfoReader(out LibHac.FsService.ISaveDataInfoReader infoReader);
  233. if (result.IsSuccess())
  234. {
  235. MakeObject(context, new ISaveDataInfoReader(infoReader));
  236. }
  237. return (ResultCode)result.Value;
  238. }
  239. [Command(61)]
  240. public ResultCode OpenSaveDataInfoReaderBySaveDataSpaceId(ServiceCtx context)
  241. {
  242. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadByte();
  243. Result result = _baseFileSystemProxy.OpenSaveDataInfoReaderBySaveDataSpaceId(out LibHac.FsService.ISaveDataInfoReader infoReader, spaceId);
  244. if (result.IsSuccess())
  245. {
  246. MakeObject(context, new ISaveDataInfoReader(infoReader));
  247. }
  248. return (ResultCode)result.Value;
  249. }
  250. [Command(67)]
  251. public ResultCode FindSaveDataWithFilter(ServiceCtx context)
  252. {
  253. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  254. SaveDataFilter filter = context.RequestData.ReadStruct<SaveDataFilter>();
  255. long bufferPosition = context.Request.ReceiveBuff[0].Position;
  256. long bufferLen = context.Request.ReceiveBuff[0].Size;
  257. byte[] infoBuffer = new byte[bufferLen];
  258. Result result = _baseFileSystemProxy.FindSaveDataWithFilter(out long count, infoBuffer, spaceId, ref filter);
  259. context.Memory.Write((ulong)bufferPosition, infoBuffer);
  260. context.ResponseData.Write(count);
  261. return (ResultCode)result.Value;
  262. }
  263. [Command(68)]
  264. public ResultCode OpenSaveDataInfoReaderWithFilter(ServiceCtx context)
  265. {
  266. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  267. SaveDataFilter filter = context.RequestData.ReadStruct<SaveDataFilter>();
  268. Result result = _baseFileSystemProxy.OpenSaveDataInfoReaderWithFilter(out LibHac.FsService.ISaveDataInfoReader infoReader, spaceId, ref filter);
  269. if (result.IsSuccess())
  270. {
  271. MakeObject(context, new ISaveDataInfoReader(infoReader));
  272. }
  273. return (ResultCode)result.Value;
  274. }
  275. [Command(200)]
  276. // OpenDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage> dataStorage
  277. public ResultCode OpenDataStorageByCurrentProcess(ServiceCtx context)
  278. {
  279. MakeObject(context, new FileSystemProxy.IStorage(context.Device.FileSystem.RomFs.AsStorage()));
  280. return 0;
  281. }
  282. [Command(202)]
  283. // OpenDataStorageByDataId(u8 storageId, nn::ApplicationId tid) -> object<nn::fssrv::sf::IStorage> dataStorage
  284. public ResultCode OpenDataStorageByDataId(ServiceCtx context)
  285. {
  286. StorageId storageId = (StorageId)context.RequestData.ReadByte();
  287. byte[] padding = context.RequestData.ReadBytes(7);
  288. long titleId = context.RequestData.ReadInt64();
  289. // We do a mitm here to find if the request is for an AOC.
  290. // This is because AOC can be distributed over multiple containers in the emulator.
  291. if (context.Device.System.ContentManager.GetAocDataStorage((ulong)titleId, out LibHac.Fs.IStorage aocStorage))
  292. {
  293. Logger.PrintInfo(LogClass.Loader, $"Opened AddOnContent Data TitleID={titleId:X16}");
  294. MakeObject(context, new FileSystemProxy.IStorage(aocStorage));
  295. return ResultCode.Success;
  296. }
  297. NcaContentType contentType = NcaContentType.Data;
  298. StorageId installedStorage = context.Device.System.ContentManager.GetInstalledStorage(titleId, contentType, storageId);
  299. if (installedStorage == StorageId.None)
  300. {
  301. contentType = NcaContentType.PublicData;
  302. installedStorage = context.Device.System.ContentManager.GetInstalledStorage(titleId, contentType, storageId);
  303. }
  304. if (installedStorage != StorageId.None)
  305. {
  306. string contentPath = context.Device.System.ContentManager.GetInstalledContentPath(titleId, storageId, contentType);
  307. string installPath = context.Device.FileSystem.SwitchPathToSystemPath(contentPath);
  308. if (!string.IsNullOrWhiteSpace(installPath))
  309. {
  310. string ncaPath = installPath;
  311. if (File.Exists(ncaPath))
  312. {
  313. try
  314. {
  315. LibHac.Fs.IStorage ncaStorage = new LocalStorage(ncaPath, FileAccess.Read, FileMode.Open);
  316. Nca nca = new Nca(context.Device.System.KeySet, ncaStorage);
  317. LibHac.Fs.IStorage romfsStorage = nca.OpenStorage(NcaSectionType.Data, context.Device.System.FsIntegrityCheckLevel);
  318. MakeObject(context, new FileSystemProxy.IStorage(romfsStorage));
  319. }
  320. catch (HorizonResultException ex)
  321. {
  322. return (ResultCode)ex.ResultValue.Value;
  323. }
  324. return ResultCode.Success;
  325. }
  326. else
  327. {
  328. throw new FileNotFoundException($"No Nca found in Path `{ncaPath}`.");
  329. }
  330. }
  331. else
  332. {
  333. throw new DirectoryNotFoundException($"Path for title id {titleId:x16} on Storage {storageId} was not found in Path {installPath}.");
  334. }
  335. }
  336. throw new FileNotFoundException($"System archive with titleid {titleId:x16} was not found on Storage {storageId}. Found in {installedStorage}.");
  337. }
  338. [Command(203)]
  339. // OpenPatchDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage>
  340. public ResultCode OpenPatchDataStorageByCurrentProcess(ServiceCtx context)
  341. {
  342. MakeObject(context, new FileSystemProxy.IStorage(context.Device.FileSystem.RomFs.AsStorage()));
  343. return ResultCode.Success;
  344. }
  345. [Command(400)]
  346. // OpenDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage> dataStorage
  347. public ResultCode OpenDeviceOperator(ServiceCtx context)
  348. {
  349. Result result = _baseFileSystemProxy.OpenDeviceOperator(out LibHac.FsService.IDeviceOperator deviceOperator);
  350. if (result.IsSuccess())
  351. {
  352. MakeObject(context, new IDeviceOperator(deviceOperator));
  353. }
  354. return (ResultCode)result.Value;
  355. }
  356. [Command(630)]
  357. // SetSdCardAccessibility(u8)
  358. public ResultCode SetSdCardAccessibility(ServiceCtx context)
  359. {
  360. bool isAccessible = context.RequestData.ReadBoolean();
  361. return (ResultCode)_baseFileSystemProxy.SetSdCardAccessibility(isAccessible).Value;
  362. }
  363. [Command(631)]
  364. // IsSdCardAccessible() -> u8
  365. public ResultCode IsSdCardAccessible(ServiceCtx context)
  366. {
  367. Result result = _baseFileSystemProxy.IsSdCardAccessible(out bool isAccessible);
  368. context.ResponseData.Write(isAccessible);
  369. return (ResultCode)result.Value;
  370. }
  371. [Command(1004)]
  372. // SetGlobalAccessLogMode(u32 mode)
  373. public ResultCode SetGlobalAccessLogMode(ServiceCtx context)
  374. {
  375. int mode = context.RequestData.ReadInt32();
  376. context.Device.System.GlobalAccessLogMode = mode;
  377. return ResultCode.Success;
  378. }
  379. [Command(1005)]
  380. // GetGlobalAccessLogMode() -> u32 logMode
  381. public ResultCode GetGlobalAccessLogMode(ServiceCtx context)
  382. {
  383. int mode = context.Device.System.GlobalAccessLogMode;
  384. context.ResponseData.Write(mode);
  385. return ResultCode.Success;
  386. }
  387. [Command(1006)]
  388. // OutputAccessLogToSdCard(buffer<bytes, 5> log_text)
  389. public ResultCode OutputAccessLogToSdCard(ServiceCtx context)
  390. {
  391. string message = ReadUtf8StringSend(context);
  392. // FS ends each line with a newline. Remove it because Ryujinx logging adds its own newline
  393. Logger.PrintAccessLog(LogClass.ServiceFs, message.TrimEnd('\n'));
  394. return ResultCode.Success;
  395. }
  396. [Command(1011)]
  397. public ResultCode GetProgramIndexForAccessLog(ServiceCtx context)
  398. {
  399. int programIndex = 0;
  400. int programCount = 1;
  401. context.ResponseData.Write(programIndex);
  402. context.ResponseData.Write(programCount);
  403. return ResultCode.Success;
  404. }
  405. [Command(1200)] // 6.0.0+
  406. // OpenMultiCommitManager() -> object<nn::fssrv::sf::IMultiCommitManager>
  407. public ResultCode OpenMultiCommitManager(ServiceCtx context)
  408. {
  409. Result result = _baseFileSystemProxy.OpenMultiCommitManager(out LibHac.FsService.IMultiCommitManager commitManager);
  410. if (result.IsSuccess())
  411. {
  412. MakeObject(context, new IMultiCommitManager(commitManager));
  413. }
  414. return (ResultCode)result.Value;
  415. }
  416. }
  417. }