IFileSystemProxy.cs 54 KB

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