IFileSystemProxy.cs 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. using LibHac;
  2. using LibHac.Fs;
  3. using LibHac.Fs.Shim;
  4. using LibHac.FsSrv;
  5. using LibHac.FsSrv.Impl;
  6. using LibHac.FsSystem;
  7. using LibHac.FsSystem.NcaUtils;
  8. using LibHac.Ncm;
  9. using LibHac.Sf;
  10. using LibHac.Spl;
  11. using Ryujinx.Common;
  12. using Ryujinx.Common.Logging;
  13. using Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy;
  14. using System.IO;
  15. using static Ryujinx.HLE.Utilities.StringUtils;
  16. using IFileSystem = LibHac.FsSrv.Sf.IFileSystem;
  17. using IStorage = LibHac.FsSrv.Sf.IStorage;
  18. using RightsId = LibHac.Fs.RightsId;
  19. using StorageId = Ryujinx.HLE.FileSystem.StorageId;
  20. namespace Ryujinx.HLE.HOS.Services.Fs
  21. {
  22. [Service("fsp-srv")]
  23. class IFileSystemProxy : DisposableIpcService
  24. {
  25. private ReferenceCountedDisposable<LibHac.FsSrv.Sf.IFileSystemProxy> _baseFileSystemProxy;
  26. public IFileSystemProxy(ServiceCtx context)
  27. {
  28. var applicationClient = context.Device.System.LibHacHorizonManager.ApplicationClient;
  29. _baseFileSystemProxy = applicationClient.Fs.Impl.GetFileSystemProxyServiceObject();
  30. }
  31. [CommandHipc(1)]
  32. // SetCurrentProcess(u64, pid)
  33. public ResultCode SetCurrentProcess(ServiceCtx context)
  34. {
  35. return ResultCode.Success;
  36. }
  37. [CommandHipc(8)]
  38. // OpenFileSystemWithId(nn::fssrv::sf::FileSystemType filesystem_type, nn::ApplicationId tid, buffer<bytes<0x301>, 0x19, 0x301> path)
  39. // -> object<nn::fssrv::sf::IFileSystem> contentFs
  40. public ResultCode OpenFileSystemWithId(ServiceCtx context)
  41. {
  42. FileSystemType fileSystemType = (FileSystemType)context.RequestData.ReadInt32();
  43. ulong titleId = context.RequestData.ReadUInt64();
  44. string switchPath = ReadUtf8String(context);
  45. string fullPath = context.Device.FileSystem.SwitchPathToSystemPath(switchPath);
  46. if (!File.Exists(fullPath))
  47. {
  48. if (fullPath.Contains("."))
  49. {
  50. ResultCode result = FileSystemProxyHelper.OpenFileSystemFromInternalFile(context, fullPath, out FileSystemProxy.IFileSystem fileSystem);
  51. if (result == ResultCode.Success)
  52. {
  53. MakeObject(context, fileSystem);
  54. }
  55. return result;
  56. }
  57. return ResultCode.PathDoesNotExist;
  58. }
  59. FileStream fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
  60. string extension = Path.GetExtension(fullPath);
  61. if (extension == ".nca")
  62. {
  63. ResultCode result = FileSystemProxyHelper.OpenNcaFs(context, fullPath, fileStream.AsStorage(), out FileSystemProxy.IFileSystem fileSystem);
  64. if (result == ResultCode.Success)
  65. {
  66. MakeObject(context, fileSystem);
  67. }
  68. return result;
  69. }
  70. else if (extension == ".nsp")
  71. {
  72. ResultCode result = FileSystemProxyHelper.OpenNsp(context, fullPath, out FileSystemProxy.IFileSystem fileSystem);
  73. if (result == ResultCode.Success)
  74. {
  75. MakeObject(context, fileSystem);
  76. }
  77. return result;
  78. }
  79. return ResultCode.InvalidInput;
  80. }
  81. [CommandHipc(11)]
  82. // OpenBisFileSystem(nn::fssrv::sf::Partition partitionID, buffer<bytes<0x301>, 0x19, 0x301>) -> object<nn::fssrv::sf::IFileSystem> Bis
  83. public ResultCode OpenBisFileSystem(ServiceCtx context)
  84. {
  85. BisPartitionId bisPartitionId = (BisPartitionId)context.RequestData.ReadInt32();
  86. ref readonly var path = ref FileSystemProxyHelper.GetFspPath(context);
  87. Result result = _baseFileSystemProxy.Target.OpenBisFileSystem(out ReferenceCountedDisposable<IFileSystem> fileSystem, in path, bisPartitionId);
  88. if (result.IsFailure()) return (ResultCode)result.Value;
  89. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  90. return ResultCode.Success;
  91. }
  92. [CommandHipc(12)]
  93. // OpenBisStorage(u32 partitionId) -> object<nn::fssrv::sf::IStorage> bisStorage
  94. public ResultCode OpenBisStorage(ServiceCtx context)
  95. {
  96. BisPartitionId bisPartitionId = (BisPartitionId)context.RequestData.ReadInt32();
  97. Result result = _baseFileSystemProxy.Target.OpenBisStorage(out ReferenceCountedDisposable<IStorage> storage, bisPartitionId);
  98. if (result.IsFailure()) return (ResultCode)result.Value;
  99. MakeObject(context, new FileSystemProxy.IStorage(storage));
  100. return ResultCode.Success;
  101. }
  102. [CommandHipc(13)]
  103. // InvalidateBisCache() -> ()
  104. public ResultCode InvalidateBisCache(ServiceCtx context)
  105. {
  106. return (ResultCode)_baseFileSystemProxy.Target.InvalidateBisCache().Value;
  107. }
  108. [CommandHipc(18)]
  109. // OpenSdCardFileSystem() -> object<nn::fssrv::sf::IFileSystem>
  110. public ResultCode OpenSdCardFileSystem(ServiceCtx context)
  111. {
  112. Result result = _baseFileSystemProxy.Target.OpenSdCardFileSystem(out var fileSystem);
  113. if (result.IsFailure()) return (ResultCode)result.Value;
  114. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  115. return ResultCode.Success;
  116. }
  117. [CommandHipc(19)]
  118. // FormatSdCardFileSystem() -> ()
  119. public ResultCode FormatSdCardFileSystem(ServiceCtx context)
  120. {
  121. return (ResultCode)_baseFileSystemProxy.Target.FormatSdCardFileSystem().Value;
  122. }
  123. [CommandHipc(21)]
  124. // DeleteSaveDataFileSystem(u64 saveDataId) -> ()
  125. public ResultCode DeleteSaveDataFileSystem(ServiceCtx context)
  126. {
  127. ulong saveDataId = context.RequestData.ReadUInt64();
  128. return (ResultCode)_baseFileSystemProxy.Target.DeleteSaveDataFileSystem(saveDataId).Value;
  129. }
  130. [CommandHipc(22)]
  131. // CreateSaveDataFileSystem(nn::fs::SaveDataAttribute attribute, nn::fs::SaveDataCreationInfo creationInfo, nn::fs::SaveDataMetaInfo metaInfo) -> ()
  132. public ResultCode CreateSaveDataFileSystem(ServiceCtx context)
  133. {
  134. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  135. SaveDataCreationInfo creationInfo = context.RequestData.ReadStruct<SaveDataCreationInfo>();
  136. SaveDataMetaInfo metaInfo = context.RequestData.ReadStruct<SaveDataMetaInfo>();
  137. return (ResultCode)_baseFileSystemProxy.Target.CreateSaveDataFileSystem(in attribute, in creationInfo, in metaInfo).Value;
  138. }
  139. [CommandHipc(23)]
  140. // CreateSaveDataFileSystemBySystemSaveDataId(nn::fs::SaveDataAttribute attribute, nn::fs::SaveDataCreationInfo creationInfo) -> ()
  141. public ResultCode CreateSaveDataFileSystemBySystemSaveDataId(ServiceCtx context)
  142. {
  143. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  144. SaveDataCreationInfo creationInfo = context.RequestData.ReadStruct<SaveDataCreationInfo>();
  145. return (ResultCode)_baseFileSystemProxy.Target.CreateSaveDataFileSystemBySystemSaveDataId(in attribute, in creationInfo).Value;
  146. }
  147. [CommandHipc(24)]
  148. // RegisterSaveDataFileSystemAtomicDeletion(buffer<u64, 5> saveDataIds) -> ()
  149. public ResultCode RegisterSaveDataFileSystemAtomicDeletion(ServiceCtx context)
  150. {
  151. byte[] saveIdBuffer = new byte[context.Request.SendBuff[0].Size];
  152. context.Memory.Read(context.Request.SendBuff[0].Position, saveIdBuffer);
  153. return (ResultCode)_baseFileSystemProxy.Target.RegisterSaveDataFileSystemAtomicDeletion(new InBuffer(saveIdBuffer)).Value;
  154. }
  155. [CommandHipc(25)]
  156. // DeleteSaveDataFileSystemBySaveDataSpaceId(u8 spaceId, u64 saveDataId) -> ()
  157. public ResultCode DeleteSaveDataFileSystemBySaveDataSpaceId(ServiceCtx context)
  158. {
  159. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  160. ulong saveDataId = context.RequestData.ReadUInt64();
  161. return (ResultCode)_baseFileSystemProxy.Target.DeleteSaveDataFileSystemBySaveDataSpaceId(spaceId, saveDataId).Value;
  162. }
  163. [CommandHipc(26)]
  164. // FormatSdCardDryRun() -> ()
  165. public ResultCode FormatSdCardDryRun(ServiceCtx context)
  166. {
  167. return (ResultCode)_baseFileSystemProxy.Target.FormatSdCardDryRun().Value;
  168. }
  169. [CommandHipc(27)]
  170. // IsExFatSupported() -> (u8 isSupported)
  171. public ResultCode IsExFatSupported(ServiceCtx context)
  172. {
  173. Result result = _baseFileSystemProxy.Target.IsExFatSupported(out bool isSupported);
  174. if (result.IsFailure()) return (ResultCode)result.Value;
  175. context.ResponseData.Write(isSupported);
  176. return ResultCode.Success;
  177. }
  178. [CommandHipc(28)]
  179. // DeleteSaveDataFileSystemBySaveDataAttribute(u8 spaceId, nn::fs::SaveDataAttribute attribute) -> ()
  180. public ResultCode DeleteSaveDataFileSystemBySaveDataAttribute(ServiceCtx context)
  181. {
  182. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  183. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  184. return (ResultCode)_baseFileSystemProxy.Target.DeleteSaveDataFileSystemBySaveDataAttribute(spaceId, in attribute).Value;
  185. }
  186. [CommandHipc(30)]
  187. // OpenGameCardStorage(u32 handle, u32 partitionId) -> object<nn::fssrv::sf::IStorage>
  188. public ResultCode OpenGameCardStorage(ServiceCtx context)
  189. {
  190. GameCardHandle handle = new GameCardHandle(context.RequestData.ReadInt32());
  191. GameCardPartitionRaw partitionId = (GameCardPartitionRaw)context.RequestData.ReadInt32();
  192. Result result = _baseFileSystemProxy.Target.OpenGameCardStorage(out ReferenceCountedDisposable<IStorage> storage, handle, partitionId);
  193. if (result.IsFailure()) return (ResultCode)result.Value;
  194. MakeObject(context, new FileSystemProxy.IStorage(storage));
  195. return ResultCode.Success;
  196. }
  197. [CommandHipc(31)]
  198. // OpenGameCardFileSystem(u32 handle, u32 partitionId) -> object<nn::fssrv::sf::IFileSystem>
  199. public ResultCode OpenGameCardFileSystem(ServiceCtx context)
  200. {
  201. GameCardHandle handle = new GameCardHandle(context.RequestData.ReadInt32());
  202. GameCardPartition partitionId = (GameCardPartition)context.RequestData.ReadInt32();
  203. Result result = _baseFileSystemProxy.Target.OpenGameCardFileSystem(out ReferenceCountedDisposable<IFileSystem> fileSystem, handle, partitionId);
  204. if (result.IsFailure()) return (ResultCode)result.Value;
  205. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  206. return ResultCode.Success;
  207. }
  208. [CommandHipc(32)]
  209. // ExtendSaveDataFileSystem(u8 spaceId, u64 saveDataId, s64 dataSize, s64 journalSize) -> ()
  210. public ResultCode ExtendSaveDataFileSystem(ServiceCtx context)
  211. {
  212. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  213. ulong saveDataId = context.RequestData.ReadUInt64();
  214. long dataSize = context.RequestData.ReadInt64();
  215. long journalSize = context.RequestData.ReadInt64();
  216. return (ResultCode)_baseFileSystemProxy.Target.ExtendSaveDataFileSystem(spaceId, saveDataId, dataSize, journalSize).Value;
  217. }
  218. [CommandHipc(33)]
  219. // DeleteCacheStorage(u16 index) -> ()
  220. public ResultCode DeleteCacheStorage(ServiceCtx context)
  221. {
  222. ushort index = context.RequestData.ReadUInt16();
  223. return (ResultCode)_baseFileSystemProxy.Target.DeleteCacheStorage(index).Value;
  224. }
  225. [CommandHipc(34)]
  226. // GetCacheStorageSize(u16 index) -> (s64 dataSize, s64 journalSize)
  227. public ResultCode GetCacheStorageSize(ServiceCtx context)
  228. {
  229. ushort index = context.RequestData.ReadUInt16();
  230. Result result = _baseFileSystemProxy.Target.GetCacheStorageSize(out long dataSize, out long journalSize, index);
  231. if (result.IsFailure()) return (ResultCode)result.Value;
  232. context.ResponseData.Write(dataSize);
  233. context.ResponseData.Write(journalSize);
  234. return ResultCode.Success;
  235. }
  236. [CommandHipc(35)]
  237. // CreateSaveDataFileSystemWithHashSalt(nn::fs::SaveDataAttribute attribute, nn::fs::SaveDataCreationInfo creationInfo, nn::fs::SaveDataMetaInfo metaInfo nn::fs::HashSalt hashSalt) -> ()
  238. public ResultCode CreateSaveDataFileSystemWithHashSalt(ServiceCtx context)
  239. {
  240. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  241. SaveDataCreationInfo creationInfo = context.RequestData.ReadStruct<SaveDataCreationInfo>();
  242. SaveDataMetaInfo metaCreateInfo = context.RequestData.ReadStruct<SaveDataMetaInfo>();
  243. HashSalt hashSalt = context.RequestData.ReadStruct<HashSalt>();
  244. return (ResultCode)_baseFileSystemProxy.Target.CreateSaveDataFileSystemWithHashSalt(in attribute, in creationInfo, in metaCreateInfo, in hashSalt).Value;
  245. }
  246. [CommandHipc(51)]
  247. // OpenSaveDataFileSystem(u8 spaceId, nn::fs::SaveDataAttribute attribute) -> object<nn::fssrv::sf::IFileSystem> saveDataFs
  248. public ResultCode OpenSaveDataFileSystem(ServiceCtx context)
  249. {
  250. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  251. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  252. Result result = _baseFileSystemProxy.Target.OpenSaveDataFileSystem(out ReferenceCountedDisposable<IFileSystem> fileSystem, spaceId, in attribute);
  253. if (result.IsFailure()) return (ResultCode)result.Value;
  254. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  255. return ResultCode.Success;
  256. }
  257. [CommandHipc(52)]
  258. // OpenSaveDataFileSystemBySystemSaveDataId(u8 spaceId, nn::fs::SaveDataAttribute attribute) -> object<nn::fssrv::sf::IFileSystem> systemSaveDataFs
  259. public ResultCode OpenSaveDataFileSystemBySystemSaveDataId(ServiceCtx context)
  260. {
  261. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  262. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  263. Result result = _baseFileSystemProxy.Target.OpenSaveDataFileSystemBySystemSaveDataId(out ReferenceCountedDisposable<IFileSystem> fileSystem, spaceId, in attribute);
  264. if (result.IsFailure()) return (ResultCode)result.Value;
  265. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  266. return ResultCode.Success;
  267. }
  268. [CommandHipc(53)]
  269. // OpenReadOnlySaveDataFileSystem(u8 spaceId, nn::fs::SaveDataAttribute attribute) -> object<nn::fssrv::sf::IFileSystem>
  270. public ResultCode OpenReadOnlySaveDataFileSystem(ServiceCtx context)
  271. {
  272. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  273. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  274. Result result = _baseFileSystemProxy.Target.OpenReadOnlySaveDataFileSystem(out ReferenceCountedDisposable<IFileSystem> fileSystem, spaceId, in attribute);
  275. if (result.IsFailure()) return (ResultCode)result.Value;
  276. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  277. return ResultCode.Success;
  278. }
  279. [CommandHipc(57)]
  280. // ReadSaveDataFileSystemExtraDataBySaveDataSpaceId(u8 spaceId, u64 saveDataId) -> (buffer<nn::fs::SaveDataExtraData, 6> extraData)
  281. public ResultCode ReadSaveDataFileSystemExtraDataBySaveDataSpaceId(ServiceCtx context)
  282. {
  283. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  284. ulong saveDataId = context.RequestData.ReadUInt64();
  285. byte[] extraDataBuffer = new byte[context.Request.ReceiveBuff[0].Size];
  286. context.Memory.Read(context.Request.ReceiveBuff[0].Position, extraDataBuffer);
  287. Result result = _baseFileSystemProxy.Target.ReadSaveDataFileSystemExtraDataBySaveDataSpaceId(new OutBuffer(extraDataBuffer), spaceId, saveDataId);
  288. if (result.IsFailure()) return (ResultCode)result.Value;
  289. context.Memory.Write(context.Request.ReceiveBuff[0].Position, extraDataBuffer);
  290. return ResultCode.Success;
  291. }
  292. [CommandHipc(58)]
  293. // ReadSaveDataFileSystemExtraData(u64 saveDataId) -> (buffer<nn::fs::SaveDataExtraData, 6> extraData)
  294. public ResultCode ReadSaveDataFileSystemExtraData(ServiceCtx context)
  295. {
  296. ulong saveDataId = context.RequestData.ReadUInt64();
  297. byte[] extraDataBuffer = new byte[context.Request.ReceiveBuff[0].Size];
  298. context.Memory.Read(context.Request.ReceiveBuff[0].Position, extraDataBuffer);
  299. Result result = _baseFileSystemProxy.Target.ReadSaveDataFileSystemExtraData(new OutBuffer(extraDataBuffer), saveDataId);
  300. if (result.IsFailure()) return (ResultCode)result.Value;
  301. context.Memory.Write(context.Request.ReceiveBuff[0].Position, extraDataBuffer);
  302. return ResultCode.Success;
  303. }
  304. [CommandHipc(59)]
  305. // WriteSaveDataFileSystemExtraData(u8 spaceId, u64 saveDataId, buffer<nn::fs::SaveDataExtraData, 5> extraData) -> ()
  306. public ResultCode WriteSaveDataFileSystemExtraData(ServiceCtx context)
  307. {
  308. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  309. ulong saveDataId = context.RequestData.ReadUInt64();
  310. byte[] extraDataBuffer = new byte[context.Request.SendBuff[0].Size];
  311. context.Memory.Read(context.Request.SendBuff[0].Position, extraDataBuffer);
  312. return (ResultCode)_baseFileSystemProxy.Target.WriteSaveDataFileSystemExtraData(saveDataId, spaceId, new InBuffer(extraDataBuffer)).Value;
  313. }
  314. [CommandHipc(60)]
  315. // OpenSaveDataInfoReader() -> object<nn::fssrv::sf::ISaveDataInfoReader>
  316. public ResultCode OpenSaveDataInfoReader(ServiceCtx context)
  317. {
  318. Result result = _baseFileSystemProxy.Target.OpenSaveDataInfoReader(out ReferenceCountedDisposable<LibHac.FsSrv.Sf.ISaveDataInfoReader> infoReader);
  319. if (result.IsFailure()) return (ResultCode)result.Value;
  320. MakeObject(context, new ISaveDataInfoReader(infoReader));
  321. return ResultCode.Success;
  322. }
  323. [CommandHipc(61)]
  324. // OpenSaveDataInfoReaderBySaveDataSpaceId(u8 spaceId) -> object<nn::fssrv::sf::ISaveDataInfoReader>
  325. public ResultCode OpenSaveDataInfoReaderBySaveDataSpaceId(ServiceCtx context)
  326. {
  327. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadByte();
  328. Result result = _baseFileSystemProxy.Target.OpenSaveDataInfoReaderBySaveDataSpaceId(out ReferenceCountedDisposable<LibHac.FsSrv.Sf.ISaveDataInfoReader> infoReader, spaceId);
  329. if (result.IsFailure()) return (ResultCode)result.Value;
  330. MakeObject(context, new ISaveDataInfoReader(infoReader));
  331. return ResultCode.Success;
  332. }
  333. [CommandHipc(62)]
  334. // OpenSaveDataInfoReaderOnlyCacheStorage() -> object<nn::fssrv::sf::ISaveDataInfoReader>
  335. public ResultCode OpenSaveDataInfoReaderOnlyCacheStorage(ServiceCtx context)
  336. {
  337. Result result = _baseFileSystemProxy.Target.OpenSaveDataInfoReaderOnlyCacheStorage(out ReferenceCountedDisposable<LibHac.FsSrv.Sf.ISaveDataInfoReader> infoReader);
  338. if (result.IsFailure()) return (ResultCode)result.Value;
  339. MakeObject(context, new ISaveDataInfoReader(infoReader));
  340. return ResultCode.Success;
  341. }
  342. [CommandHipc(64)]
  343. // OpenSaveDataInternalStorageFileSystem(u8 spaceId, u64 saveDataId) -> object<nn::fssrv::sf::ISaveDataInfoReader>
  344. public ResultCode OpenSaveDataInternalStorageFileSystem(ServiceCtx context)
  345. {
  346. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  347. ulong saveDataId = context.RequestData.ReadUInt64();
  348. Result result = _baseFileSystemProxy.Target.OpenSaveDataInternalStorageFileSystem(out ReferenceCountedDisposable<IFileSystem> fileSystem, spaceId, saveDataId);
  349. if (result.IsFailure()) return (ResultCode)result.Value;
  350. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  351. return ResultCode.Success;
  352. }
  353. [CommandHipc(65)]
  354. // UpdateSaveDataMacForDebug(u8 spaceId, u64 saveDataId) -> ()
  355. public ResultCode UpdateSaveDataMacForDebug(ServiceCtx context)
  356. {
  357. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  358. ulong saveDataId = context.RequestData.ReadUInt64();
  359. return (ResultCode)_baseFileSystemProxy.Target.UpdateSaveDataMacForDebug(spaceId, saveDataId).Value;
  360. }
  361. [CommandHipc(66)]
  362. public ResultCode WriteSaveDataFileSystemExtraDataWithMask(ServiceCtx context)
  363. {
  364. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  365. ulong saveDataId = context.RequestData.ReadUInt64();
  366. byte[] extraDataBuffer = new byte[context.Request.SendBuff[0].Size];
  367. context.Memory.Read(context.Request.SendBuff[0].Position, extraDataBuffer);
  368. byte[] maskBuffer = new byte[context.Request.SendBuff[1].Size];
  369. context.Memory.Read(context.Request.SendBuff[1].Position, maskBuffer);
  370. return (ResultCode)_baseFileSystemProxy.Target.WriteSaveDataFileSystemExtraDataWithMask(saveDataId, spaceId, new InBuffer(extraDataBuffer), new InBuffer(maskBuffer)).Value;
  371. }
  372. [CommandHipc(67)]
  373. public ResultCode FindSaveDataWithFilter(ServiceCtx context)
  374. {
  375. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  376. SaveDataFilter filter = context.RequestData.ReadStruct<SaveDataFilter>();
  377. ulong bufferPosition = context.Request.ReceiveBuff[0].Position;
  378. ulong bufferLen = context.Request.ReceiveBuff[0].Size;
  379. byte[] infoBuffer = new byte[bufferLen];
  380. Result result = _baseFileSystemProxy.Target.FindSaveDataWithFilter(out long count, new OutBuffer(infoBuffer), spaceId, in filter);
  381. if (result.IsFailure()) return (ResultCode)result.Value;
  382. context.Memory.Write(bufferPosition, infoBuffer);
  383. context.ResponseData.Write(count);
  384. return ResultCode.Success;
  385. }
  386. [CommandHipc(68)]
  387. public ResultCode OpenSaveDataInfoReaderWithFilter(ServiceCtx context)
  388. {
  389. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  390. SaveDataFilter filter = context.RequestData.ReadStruct<SaveDataFilter>();
  391. Result result = _baseFileSystemProxy.Target.OpenSaveDataInfoReaderWithFilter(out ReferenceCountedDisposable<LibHac.FsSrv.Sf.ISaveDataInfoReader> infoReader, spaceId, in filter);
  392. if (result.IsFailure()) return (ResultCode)result.Value;
  393. MakeObject(context, new ISaveDataInfoReader(infoReader));
  394. return ResultCode.Success;
  395. }
  396. [CommandHipc(69)]
  397. public ResultCode ReadSaveDataFileSystemExtraDataBySaveDataAttribute(ServiceCtx context)
  398. {
  399. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  400. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  401. byte[] outputBuffer = new byte[context.Request.ReceiveBuff[0].Size];
  402. context.Memory.Read(context.Request.ReceiveBuff[0].Position, outputBuffer);
  403. Result result = _baseFileSystemProxy.Target.ReadSaveDataFileSystemExtraDataBySaveDataAttribute(new OutBuffer(outputBuffer), spaceId, in attribute);
  404. if (result.IsFailure()) return (ResultCode)result.Value;
  405. context.Memory.Write(context.Request.ReceiveBuff[0].Position, outputBuffer);
  406. return ResultCode.Success;
  407. }
  408. [CommandHipc(70)]
  409. public ResultCode WriteSaveDataFileSystemExtraDataWithMaskBySaveDataAttribute(ServiceCtx context)
  410. {
  411. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  412. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  413. byte[] extraDataBuffer = new byte[context.Request.SendBuff[0].Size];
  414. context.Memory.Read(context.Request.SendBuff[0].Position, extraDataBuffer);
  415. byte[] maskBuffer = new byte[context.Request.SendBuff[1].Size];
  416. context.Memory.Read(context.Request.SendBuff[1].Position, maskBuffer);
  417. return (ResultCode)_baseFileSystemProxy.Target.WriteSaveDataFileSystemExtraDataWithMaskBySaveDataAttribute(in attribute, spaceId, new InBuffer(extraDataBuffer), new InBuffer(maskBuffer)).Value;
  418. }
  419. [CommandHipc(71)]
  420. public ResultCode ReadSaveDataFileSystemExtraDataWithMaskBySaveDataAttribute(ServiceCtx context)
  421. {
  422. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  423. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  424. byte[] maskBuffer = new byte[context.Request.SendBuff[0].Size];
  425. context.Memory.Read(context.Request.SendBuff[0].Position, maskBuffer);
  426. byte[] outputBuffer = new byte[context.Request.ReceiveBuff[0].Size];
  427. context.Memory.Read(context.Request.ReceiveBuff[0].Position, outputBuffer);
  428. Result result = _baseFileSystemProxy.Target.ReadSaveDataFileSystemExtraDataWithMaskBySaveDataAttribute(new OutBuffer(outputBuffer), spaceId, in attribute, new InBuffer(maskBuffer));
  429. if (result.IsFailure()) return (ResultCode)result.Value;
  430. context.Memory.Write(context.Request.ReceiveBuff[0].Position, outputBuffer);
  431. return ResultCode.Success;
  432. }
  433. [CommandHipc(80)]
  434. public ResultCode OpenSaveDataMetaFile(ServiceCtx context)
  435. {
  436. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt32();
  437. SaveDataMetaType metaType = (SaveDataMetaType)context.RequestData.ReadInt32();
  438. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  439. Result result = _baseFileSystemProxy.Target.OpenSaveDataMetaFile(out ReferenceCountedDisposable<LibHac.FsSrv.Sf.IFile> file, spaceId, in attribute, metaType);
  440. if (result.IsFailure()) return (ResultCode)result.Value;
  441. MakeObject(context, new IFile(file));
  442. return ResultCode.Success;
  443. }
  444. [CommandHipc(84)]
  445. public ResultCode ListAccessibleSaveDataOwnerId(ServiceCtx context)
  446. {
  447. int startIndex = context.RequestData.ReadInt32();
  448. int bufferCount = context.RequestData.ReadInt32();
  449. ProgramId programId = context.RequestData.ReadStruct<ProgramId>();
  450. byte[] outputBuffer = new byte[context.Request.ReceiveBuff[0].Size];
  451. context.Memory.Read(context.Request.ReceiveBuff[0].Position, outputBuffer);
  452. Result result = _baseFileSystemProxy.Target.ListAccessibleSaveDataOwnerId(out int readCount, new OutBuffer(outputBuffer), programId, startIndex, bufferCount);
  453. if (result.IsFailure()) return (ResultCode)result.Value;
  454. context.ResponseData.Write(readCount);
  455. return ResultCode.Success;
  456. }
  457. [CommandHipc(100)]
  458. public ResultCode OpenImageDirectoryFileSystem(ServiceCtx context)
  459. {
  460. ImageDirectoryId directoryId = (ImageDirectoryId)context.RequestData.ReadInt32();
  461. Result result = _baseFileSystemProxy.Target.OpenImageDirectoryFileSystem(out ReferenceCountedDisposable<IFileSystem> fileSystem, directoryId);
  462. if (result.IsFailure()) return (ResultCode)result.Value;
  463. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  464. return ResultCode.Success;
  465. }
  466. [CommandHipc(101)]
  467. public ResultCode OpenBaseFileSystem(ServiceCtx context)
  468. {
  469. BaseFileSystemId fileSystemId = (BaseFileSystemId)context.RequestData.ReadInt32();
  470. Result result = _baseFileSystemProxy.Target.OpenBaseFileSystem(out ReferenceCountedDisposable<IFileSystem> fileSystem, fileSystemId);
  471. if (result.IsFailure()) return (ResultCode)result.Value;
  472. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  473. return ResultCode.Success;
  474. }
  475. [CommandHipc(110)]
  476. public ResultCode OpenContentStorageFileSystem(ServiceCtx context)
  477. {
  478. ContentStorageId contentStorageId = (ContentStorageId)context.RequestData.ReadInt32();
  479. Result result = _baseFileSystemProxy.Target.OpenContentStorageFileSystem(out ReferenceCountedDisposable<IFileSystem> fileSystem, contentStorageId);
  480. if (result.IsFailure()) return (ResultCode)result.Value;
  481. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  482. return ResultCode.Success;
  483. }
  484. [CommandHipc(120)]
  485. public ResultCode OpenCloudBackupWorkStorageFileSystem(ServiceCtx context)
  486. {
  487. CloudBackupWorkStorageId storageId = (CloudBackupWorkStorageId)context.RequestData.ReadInt32();
  488. Result result = _baseFileSystemProxy.Target.OpenCloudBackupWorkStorageFileSystem(out ReferenceCountedDisposable<IFileSystem> fileSystem, storageId);
  489. if (result.IsFailure()) return (ResultCode)result.Value;
  490. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  491. return ResultCode.Success;
  492. }
  493. [CommandHipc(130)]
  494. public ResultCode OpenCustomStorageFileSystem(ServiceCtx context)
  495. {
  496. CustomStorageId customStorageId = (CustomStorageId)context.RequestData.ReadInt32();
  497. Result result = _baseFileSystemProxy.Target.OpenCustomStorageFileSystem(out ReferenceCountedDisposable<IFileSystem> fileSystem, customStorageId);
  498. if (result.IsFailure()) return (ResultCode)result.Value;
  499. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  500. return ResultCode.Success;
  501. }
  502. [CommandHipc(200)]
  503. // OpenDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage> dataStorage
  504. public ResultCode OpenDataStorageByCurrentProcess(ServiceCtx context)
  505. {
  506. var storage = context.Device.FileSystem.RomFs.AsStorage(true);
  507. var sharedStorage = new ReferenceCountedDisposable<LibHac.Fs.IStorage>(storage);
  508. ReferenceCountedDisposable<IStorage> sfStorage = StorageInterfaceAdapter.CreateShared(ref sharedStorage);
  509. MakeObject(context, new FileSystemProxy.IStorage(sfStorage));
  510. return ResultCode.Success;
  511. }
  512. [CommandHipc(202)]
  513. // OpenDataStorageByDataId(u8 storageId, nn::ncm::DataId dataId) -> object<nn::fssrv::sf::IStorage> dataStorage
  514. public ResultCode OpenDataStorageByDataId(ServiceCtx context)
  515. {
  516. StorageId storageId = (StorageId)context.RequestData.ReadByte();
  517. byte[] padding = context.RequestData.ReadBytes(7);
  518. ulong titleId = context.RequestData.ReadUInt64();
  519. // We do a mitm here to find if the request is for an AOC.
  520. // This is because AOC can be distributed over multiple containers in the emulator.
  521. if (context.Device.System.ContentManager.GetAocDataStorage(titleId, out LibHac.Fs.IStorage aocStorage, context.Device.Configuration.FsIntegrityCheckLevel))
  522. {
  523. Logger.Info?.Print(LogClass.Loader, $"Opened AddOnContent Data TitleID={titleId:X16}");
  524. var storage = context.Device.FileSystem.ModLoader.ApplyRomFsMods(titleId, aocStorage);
  525. var sharedStorage = new ReferenceCountedDisposable<LibHac.Fs.IStorage>(storage);
  526. ReferenceCountedDisposable<IStorage> sfStorage = StorageInterfaceAdapter.CreateShared(ref sharedStorage);
  527. MakeObject(context, new FileSystemProxy.IStorage(sfStorage));
  528. return ResultCode.Success;
  529. }
  530. NcaContentType contentType = NcaContentType.Data;
  531. StorageId installedStorage = context.Device.System.ContentManager.GetInstalledStorage(titleId, contentType, storageId);
  532. if (installedStorage == StorageId.None)
  533. {
  534. contentType = NcaContentType.PublicData;
  535. installedStorage = context.Device.System.ContentManager.GetInstalledStorage(titleId, contentType, storageId);
  536. }
  537. if (installedStorage != StorageId.None)
  538. {
  539. string contentPath = context.Device.System.ContentManager.GetInstalledContentPath(titleId, storageId, contentType);
  540. string installPath = context.Device.FileSystem.SwitchPathToSystemPath(contentPath);
  541. if (!string.IsNullOrWhiteSpace(installPath))
  542. {
  543. string ncaPath = installPath;
  544. if (File.Exists(ncaPath))
  545. {
  546. try
  547. {
  548. LibHac.Fs.IStorage ncaStorage = new LocalStorage(ncaPath, FileAccess.Read, FileMode.Open);
  549. Nca nca = new Nca(context.Device.System.KeySet, ncaStorage);
  550. LibHac.Fs.IStorage romfsStorage = nca.OpenStorage(NcaSectionType.Data, context.Device.System.FsIntegrityCheckLevel);
  551. var sharedStorage = new ReferenceCountedDisposable<LibHac.Fs.IStorage>(romfsStorage);
  552. ReferenceCountedDisposable<IStorage> sfStorage = StorageInterfaceAdapter.CreateShared(ref sharedStorage);
  553. MakeObject(context, new FileSystemProxy.IStorage(sfStorage));
  554. }
  555. catch (HorizonResultException ex)
  556. {
  557. return (ResultCode)ex.ResultValue.Value;
  558. }
  559. return ResultCode.Success;
  560. }
  561. else
  562. {
  563. throw new FileNotFoundException($"No Nca found in Path `{ncaPath}`.");
  564. }
  565. }
  566. else
  567. {
  568. throw new DirectoryNotFoundException($"Path for title id {titleId:x16} on Storage {storageId} was not found in Path {installPath}.");
  569. }
  570. }
  571. throw new FileNotFoundException($"System archive with titleid {titleId:x16} was not found on Storage {storageId}. Found in {installedStorage}.");
  572. }
  573. [CommandHipc(203)]
  574. // OpenPatchDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage>
  575. public ResultCode OpenPatchDataStorageByCurrentProcess(ServiceCtx context)
  576. {
  577. var storage = context.Device.FileSystem.RomFs.AsStorage(true);
  578. var sharedStorage = new ReferenceCountedDisposable<LibHac.Fs.IStorage>(storage);
  579. ReferenceCountedDisposable<IStorage> sfStorage = StorageInterfaceAdapter.CreateShared(ref sharedStorage);
  580. MakeObject(context, new FileSystemProxy.IStorage(sfStorage));
  581. return ResultCode.Success;
  582. }
  583. [CommandHipc(400)]
  584. // OpenDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage> dataStorage
  585. public ResultCode OpenDeviceOperator(ServiceCtx context)
  586. {
  587. Result result = _baseFileSystemProxy.Target.OpenDeviceOperator(out ReferenceCountedDisposable<LibHac.FsSrv.Sf.IDeviceOperator> deviceOperator);
  588. if (result.IsFailure()) return (ResultCode)result.Value;
  589. MakeObject(context, new IDeviceOperator(deviceOperator));
  590. return ResultCode.Success;
  591. }
  592. [CommandHipc(601)]
  593. public ResultCode QuerySaveDataTotalSize(ServiceCtx context)
  594. {
  595. long dataSize = context.RequestData.ReadInt64();
  596. long journalSize = context.RequestData.ReadInt64();
  597. Result result = _baseFileSystemProxy.Target.QuerySaveDataTotalSize(out long totalSize, dataSize, journalSize);
  598. if (result.IsFailure()) return (ResultCode)result.Value;
  599. context.ResponseData.Write(totalSize);
  600. return ResultCode.Success;
  601. }
  602. [CommandHipc(511)]
  603. public ResultCode NotifySystemDataUpdateEvent(ServiceCtx context)
  604. {
  605. return (ResultCode)_baseFileSystemProxy.Target.NotifySystemDataUpdateEvent().Value;
  606. }
  607. [CommandHipc(523)]
  608. public ResultCode SimulateDeviceDetectionEvent(ServiceCtx context)
  609. {
  610. bool signalEvent = context.RequestData.ReadBoolean();
  611. context.RequestData.BaseStream.Seek(3, SeekOrigin.Current);
  612. SdmmcPort port = context.RequestData.ReadStruct<SdmmcPort>();
  613. SimulatingDeviceDetectionMode mode = context.RequestData.ReadStruct<SimulatingDeviceDetectionMode>();
  614. return (ResultCode)_baseFileSystemProxy.Target.SimulateDeviceDetectionEvent(port, mode, signalEvent).Value;
  615. }
  616. [CommandHipc(602)]
  617. public ResultCode VerifySaveDataFileSystem(ServiceCtx context)
  618. {
  619. ulong saveDataId = context.RequestData.ReadUInt64();
  620. byte[] readBuffer = new byte[context.Request.ReceiveBuff[0].Size];
  621. context.Memory.Read(context.Request.ReceiveBuff[0].Position, readBuffer);
  622. return (ResultCode)_baseFileSystemProxy.Target.VerifySaveDataFileSystem(saveDataId, new OutBuffer(readBuffer)).Value;
  623. }
  624. [CommandHipc(603)]
  625. public ResultCode CorruptSaveDataFileSystem(ServiceCtx context)
  626. {
  627. ulong saveDataId = context.RequestData.ReadUInt64();
  628. return (ResultCode)_baseFileSystemProxy.Target.CorruptSaveDataFileSystem(saveDataId).Value;
  629. }
  630. [CommandHipc(604)]
  631. public ResultCode CreatePaddingFile(ServiceCtx context)
  632. {
  633. long size = context.RequestData.ReadInt64();
  634. return (ResultCode)_baseFileSystemProxy.Target.CreatePaddingFile(size).Value;
  635. }
  636. [CommandHipc(605)]
  637. public ResultCode DeleteAllPaddingFiles(ServiceCtx context)
  638. {
  639. return (ResultCode)_baseFileSystemProxy.Target.DeleteAllPaddingFiles().Value;
  640. }
  641. [CommandHipc(606)]
  642. public ResultCode GetRightsId(ServiceCtx context)
  643. {
  644. LibHac.Ncm.StorageId storageId = (LibHac.Ncm.StorageId)context.RequestData.ReadInt64();
  645. ProgramId programId = context.RequestData.ReadStruct<ProgramId>();
  646. Result result = _baseFileSystemProxy.Target.GetRightsId(out RightsId rightsId, programId, storageId);
  647. if (result.IsFailure()) return (ResultCode)result.Value;
  648. context.ResponseData.WriteStruct(rightsId);
  649. return ResultCode.Success;
  650. }
  651. [CommandHipc(607)]
  652. public ResultCode RegisterExternalKey(ServiceCtx context)
  653. {
  654. RightsId rightsId = context.RequestData.ReadStruct<RightsId>();
  655. AccessKey accessKey = context.RequestData.ReadStruct<AccessKey>();
  656. return (ResultCode)_baseFileSystemProxy.Target.RegisterExternalKey(in rightsId, in accessKey).Value;
  657. }
  658. [CommandHipc(608)]
  659. public ResultCode UnregisterAllExternalKey(ServiceCtx context)
  660. {
  661. return (ResultCode)_baseFileSystemProxy.Target.UnregisterAllExternalKey().Value;
  662. }
  663. [CommandHipc(609)]
  664. public ResultCode GetRightsIdByPath(ServiceCtx context)
  665. {
  666. ref readonly var path = ref FileSystemProxyHelper.GetFspPath(context);
  667. Result result = _baseFileSystemProxy.Target.GetRightsIdByPath(out RightsId rightsId, in path);
  668. if (result.IsFailure()) return (ResultCode)result.Value;
  669. context.ResponseData.WriteStruct(rightsId);
  670. return ResultCode.Success;
  671. }
  672. [CommandHipc(610)]
  673. public ResultCode GetRightsIdAndKeyGenerationByPath(ServiceCtx context)
  674. {
  675. ref readonly var path = ref FileSystemProxyHelper.GetFspPath(context);
  676. Result result = _baseFileSystemProxy.Target.GetRightsIdAndKeyGenerationByPath(out RightsId rightsId, out byte keyGeneration, in path);
  677. if (result.IsFailure()) return (ResultCode)result.Value;
  678. context.ResponseData.Write(keyGeneration);
  679. context.ResponseData.BaseStream.Seek(7, SeekOrigin.Current);
  680. context.ResponseData.WriteStruct(rightsId);
  681. return ResultCode.Success;
  682. }
  683. [CommandHipc(611)]
  684. public ResultCode SetCurrentPosixTimeWithTimeDifference(ServiceCtx context)
  685. {
  686. int timeDifference = context.RequestData.ReadInt32();
  687. context.RequestData.BaseStream.Seek(4, SeekOrigin.Current);
  688. long time = context.RequestData.ReadInt64();
  689. return (ResultCode)_baseFileSystemProxy.Target.SetCurrentPosixTimeWithTimeDifference(time, timeDifference).Value;
  690. }
  691. [CommandHipc(612)]
  692. public ResultCode GetFreeSpaceSizeForSaveData(ServiceCtx context)
  693. {
  694. SaveDataSpaceId spaceId = context.RequestData.ReadStruct<SaveDataSpaceId>();
  695. Result result = _baseFileSystemProxy.Target.GetFreeSpaceSizeForSaveData(out long freeSpaceSize, spaceId);
  696. if (result.IsFailure()) return (ResultCode)result.Value;
  697. context.ResponseData.Write(freeSpaceSize);
  698. return ResultCode.Success;
  699. }
  700. [CommandHipc(613)]
  701. public ResultCode VerifySaveDataFileSystemBySaveDataSpaceId(ServiceCtx context)
  702. {
  703. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  704. ulong saveDataId = context.RequestData.ReadUInt64();
  705. byte[] readBuffer = new byte[context.Request.ReceiveBuff[0].Size];
  706. context.Memory.Read(context.Request.ReceiveBuff[0].Position, readBuffer);
  707. return (ResultCode)_baseFileSystemProxy.Target.VerifySaveDataFileSystemBySaveDataSpaceId(spaceId, saveDataId, new OutBuffer(readBuffer)).Value;
  708. }
  709. [CommandHipc(614)]
  710. public ResultCode CorruptSaveDataFileSystemBySaveDataSpaceId(ServiceCtx context)
  711. {
  712. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  713. ulong saveDataId = context.RequestData.ReadUInt64();
  714. return (ResultCode)_baseFileSystemProxy.Target.CorruptSaveDataFileSystemBySaveDataSpaceId(spaceId, saveDataId).Value;
  715. }
  716. [CommandHipc(615)]
  717. public ResultCode QuerySaveDataInternalStorageTotalSize(ServiceCtx context)
  718. {
  719. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  720. ulong saveDataId = context.RequestData.ReadUInt64();
  721. Result result = _baseFileSystemProxy.Target.QuerySaveDataInternalStorageTotalSize(out long size, spaceId, saveDataId);
  722. if (result.IsFailure()) return (ResultCode)result.Value;
  723. context.ResponseData.Write(size);
  724. return ResultCode.Success;
  725. }
  726. [CommandHipc(616)]
  727. public ResultCode GetSaveDataCommitId(ServiceCtx context)
  728. {
  729. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  730. ulong saveDataId = context.RequestData.ReadUInt64();
  731. Result result = _baseFileSystemProxy.Target.GetSaveDataCommitId(out long commitId, spaceId, saveDataId);
  732. if (result.IsFailure()) return (ResultCode)result.Value;
  733. context.ResponseData.Write(commitId);
  734. return ResultCode.Success;
  735. }
  736. [CommandHipc(617)]
  737. public ResultCode UnregisterExternalKey(ServiceCtx context)
  738. {
  739. RightsId rightsId = context.RequestData.ReadStruct<RightsId>();
  740. return (ResultCode)_baseFileSystemProxy.Target.UnregisterExternalKey(in rightsId).Value;
  741. }
  742. [CommandHipc(620)]
  743. public ResultCode SetSdCardEncryptionSeed(ServiceCtx context)
  744. {
  745. EncryptionSeed encryptionSeed = context.RequestData.ReadStruct<EncryptionSeed>();
  746. return (ResultCode)_baseFileSystemProxy.Target.SetSdCardEncryptionSeed(in encryptionSeed).Value;
  747. }
  748. [CommandHipc(630)]
  749. // SetSdCardAccessibility(u8 isAccessible)
  750. public ResultCode SetSdCardAccessibility(ServiceCtx context)
  751. {
  752. bool isAccessible = context.RequestData.ReadBoolean();
  753. return (ResultCode)_baseFileSystemProxy.Target.SetSdCardAccessibility(isAccessible).Value;
  754. }
  755. [CommandHipc(631)]
  756. // IsSdCardAccessible() -> u8 isAccessible
  757. public ResultCode IsSdCardAccessible(ServiceCtx context)
  758. {
  759. Result result = _baseFileSystemProxy.Target.IsSdCardAccessible(out bool isAccessible);
  760. if (result.IsFailure()) return (ResultCode)result.Value;
  761. context.ResponseData.Write(isAccessible);
  762. return ResultCode.Success;
  763. }
  764. [CommandHipc(702)]
  765. public ResultCode IsAccessFailureDetected(ServiceCtx context)
  766. {
  767. ulong processId = context.RequestData.ReadUInt64();
  768. Result result = _baseFileSystemProxy.Target.IsAccessFailureDetected(out bool isDetected, processId);
  769. if (result.IsFailure()) return (ResultCode)result.Value;
  770. context.ResponseData.Write(isDetected);
  771. return ResultCode.Success;
  772. }
  773. [CommandHipc(710)]
  774. public ResultCode ResolveAccessFailure(ServiceCtx context)
  775. {
  776. ulong processId = context.RequestData.ReadUInt64();
  777. return (ResultCode)_baseFileSystemProxy.Target.ResolveAccessFailure(processId).Value;
  778. }
  779. [CommandHipc(720)]
  780. public ResultCode AbandonAccessFailure(ServiceCtx context)
  781. {
  782. ulong processId = context.RequestData.ReadUInt64();
  783. return (ResultCode)_baseFileSystemProxy.Target.AbandonAccessFailure(processId).Value;
  784. }
  785. [CommandHipc(800)]
  786. public ResultCode GetAndClearErrorInfo(ServiceCtx context)
  787. {
  788. Result result = _baseFileSystemProxy.Target.GetAndClearErrorInfo(out FileSystemProxyErrorInfo errorInfo);
  789. if (result.IsFailure()) return (ResultCode)result.Value;
  790. context.ResponseData.WriteStruct(errorInfo);
  791. return ResultCode.Success;
  792. }
  793. [CommandHipc(810)]
  794. public ResultCode RegisterProgramIndexMapInfo(ServiceCtx context)
  795. {
  796. int programCount = context.RequestData.ReadInt32();
  797. byte[] mapInfoBuffer = new byte[context.Request.SendBuff[0].Size];
  798. context.Memory.Read(context.Request.SendBuff[0].Position, mapInfoBuffer);
  799. return (ResultCode)_baseFileSystemProxy.Target.RegisterProgramIndexMapInfo(new InBuffer(mapInfoBuffer), programCount).Value;
  800. }
  801. [CommandHipc(1000)]
  802. public ResultCode SetBisRootForHost(ServiceCtx context)
  803. {
  804. BisPartitionId partitionId = (BisPartitionId)context.RequestData.ReadInt32();
  805. ref readonly var path = ref FileSystemProxyHelper.GetFspPath(context);
  806. return (ResultCode)_baseFileSystemProxy.Target.SetBisRootForHost(partitionId, in path).Value;
  807. }
  808. [CommandHipc(1001)]
  809. public ResultCode SetSaveDataSize(ServiceCtx context)
  810. {
  811. long dataSize = context.RequestData.ReadInt64();
  812. long journalSize = context.RequestData.ReadInt64();
  813. return (ResultCode)_baseFileSystemProxy.Target.SetSaveDataSize(dataSize, journalSize).Value;
  814. }
  815. [CommandHipc(1002)]
  816. public ResultCode SetSaveDataRootPath(ServiceCtx context)
  817. {
  818. ref readonly var path = ref FileSystemProxyHelper.GetFspPath(context);
  819. return (ResultCode)_baseFileSystemProxy.Target.SetSaveDataRootPath(in path).Value;
  820. }
  821. [CommandHipc(1003)]
  822. public ResultCode DisableAutoSaveDataCreation(ServiceCtx context)
  823. {
  824. return (ResultCode)_baseFileSystemProxy.Target.DisableAutoSaveDataCreation().Value;
  825. }
  826. [CommandHipc(1004)]
  827. // SetGlobalAccessLogMode(u32 mode)
  828. public ResultCode SetGlobalAccessLogMode(ServiceCtx context)
  829. {
  830. int mode = context.RequestData.ReadInt32();
  831. context.Device.System.GlobalAccessLogMode = mode;
  832. return ResultCode.Success;
  833. }
  834. [CommandHipc(1005)]
  835. // GetGlobalAccessLogMode() -> u32 logMode
  836. public ResultCode GetGlobalAccessLogMode(ServiceCtx context)
  837. {
  838. int mode = context.Device.System.GlobalAccessLogMode;
  839. context.ResponseData.Write(mode);
  840. return ResultCode.Success;
  841. }
  842. [CommandHipc(1006)]
  843. // OutputAccessLogToSdCard(buffer<bytes, 5> log_text)
  844. public ResultCode OutputAccessLogToSdCard(ServiceCtx context)
  845. {
  846. string message = ReadUtf8StringSend(context);
  847. // FS ends each line with a newline. Remove it because Ryujinx logging adds its own newline
  848. Logger.AccessLog?.PrintMsg(LogClass.ServiceFs, message.TrimEnd('\n'));
  849. return ResultCode.Success;
  850. }
  851. [CommandHipc(1007)]
  852. public ResultCode RegisterUpdatePartition(ServiceCtx context)
  853. {
  854. return (ResultCode)_baseFileSystemProxy.Target.RegisterUpdatePartition().Value;
  855. }
  856. [CommandHipc(1008)]
  857. public ResultCode OpenRegisteredUpdatePartition(ServiceCtx context)
  858. {
  859. Result result = _baseFileSystemProxy.Target.OpenRegisteredUpdatePartition(out ReferenceCountedDisposable<IFileSystem> fileSystem);
  860. if (result.IsFailure()) return (ResultCode)result.Value;
  861. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  862. return ResultCode.Success;
  863. }
  864. [CommandHipc(1009)]
  865. public ResultCode GetAndClearMemoryReportInfo(ServiceCtx context)
  866. {
  867. Result result = _baseFileSystemProxy.Target.GetAndClearMemoryReportInfo(out MemoryReportInfo reportInfo);
  868. if (result.IsFailure()) return (ResultCode)result.Value;
  869. context.ResponseData.WriteStruct(reportInfo);
  870. return ResultCode.Success;
  871. }
  872. [CommandHipc(1011)]
  873. public ResultCode GetProgramIndexForAccessLog(ServiceCtx context)
  874. {
  875. Result result = _baseFileSystemProxy.Target.GetProgramIndexForAccessLog(out int programIndex, out int programCount);
  876. if (result.IsFailure()) return (ResultCode)result.Value;
  877. context.ResponseData.Write(programIndex);
  878. context.ResponseData.Write(programCount);
  879. return ResultCode.Success;
  880. }
  881. [CommandHipc(1012)]
  882. public ResultCode GetFsStackUsage(ServiceCtx context)
  883. {
  884. FsStackUsageThreadType threadType = context.RequestData.ReadStruct<FsStackUsageThreadType>();
  885. Result result = _baseFileSystemProxy.Target.GetFsStackUsage(out uint usage, threadType);
  886. if (result.IsFailure()) return (ResultCode)result.Value;
  887. context.ResponseData.Write(usage);
  888. return ResultCode.Success;
  889. }
  890. [CommandHipc(1013)]
  891. public ResultCode UnsetSaveDataRootPath(ServiceCtx context)
  892. {
  893. return (ResultCode)_baseFileSystemProxy.Target.UnsetSaveDataRootPath().Value;
  894. }
  895. [CommandHipc(1014)]
  896. public ResultCode OutputMultiProgramTagAccessLog(ServiceCtx context)
  897. {
  898. return (ResultCode)_baseFileSystemProxy.Target.OutputMultiProgramTagAccessLog().Value;
  899. }
  900. [CommandHipc(1016)]
  901. public ResultCode FlushAccessLogOnSdCard(ServiceCtx context)
  902. {
  903. return (ResultCode)_baseFileSystemProxy.Target.FlushAccessLogOnSdCard().Value;
  904. }
  905. [CommandHipc(1017)]
  906. public ResultCode OutputApplicationInfoAccessLog(ServiceCtx context)
  907. {
  908. ApplicationInfo info = context.RequestData.ReadStruct<ApplicationInfo>();
  909. return (ResultCode)_baseFileSystemProxy.Target.OutputApplicationInfoAccessLog(in info).Value;
  910. }
  911. [CommandHipc(1100)]
  912. public ResultCode OverrideSaveDataTransferTokenSignVerificationKey(ServiceCtx context)
  913. {
  914. byte[] keyBuffer = new byte[context.Request.SendBuff[0].Size];
  915. context.Memory.Read(context.Request.SendBuff[0].Position, keyBuffer);
  916. return (ResultCode)_baseFileSystemProxy.Target.OverrideSaveDataTransferTokenSignVerificationKey(new InBuffer(keyBuffer)).Value;
  917. }
  918. [CommandHipc(1110)]
  919. public ResultCode CorruptSaveDataFileSystemByOffset(ServiceCtx context)
  920. {
  921. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  922. ulong saveDataId = context.RequestData.ReadUInt64();
  923. long offset = context.RequestData.ReadInt64();
  924. return (ResultCode)_baseFileSystemProxy.Target.CorruptSaveDataFileSystemByOffset(spaceId, saveDataId, offset).Value;
  925. }
  926. [CommandHipc(1200)] // 6.0.0+
  927. // OpenMultiCommitManager() -> object<nn::fssrv::sf::IMultiCommitManager>
  928. public ResultCode OpenMultiCommitManager(ServiceCtx context)
  929. {
  930. Result result = _baseFileSystemProxy.Target.OpenMultiCommitManager(out ReferenceCountedDisposable<LibHac.FsSrv.Sf.IMultiCommitManager> commitManager);
  931. if (result.IsFailure()) return (ResultCode)result.Value;
  932. MakeObject(context, new IMultiCommitManager(commitManager));
  933. return ResultCode.Success;
  934. }
  935. protected override void Dispose(bool isDisposing)
  936. {
  937. if (isDisposing)
  938. {
  939. _baseFileSystemProxy?.Dispose();
  940. }
  941. }
  942. }
  943. }