IFileSystemProxy.cs 56 KB

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