IFileSystemProxy.cs 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  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 IFileSystem = LibHac.FsSrv.Sf.IFileSystem;
  19. using IStorage = LibHac.FsSrv.Sf.IStorage;
  20. using RightsId = LibHac.Fs.RightsId;
  21. namespace Ryujinx.HLE.HOS.Services.Fs
  22. {
  23. [Service("fsp-srv")]
  24. class IFileSystemProxy : DisposableIpcService
  25. {
  26. private SharedRef<LibHac.FsSrv.Sf.IFileSystemProxy> _baseFileSystemProxy;
  27. public IFileSystemProxy(ServiceCtx context) : base(context.Device.System.FsServer)
  28. {
  29. var applicationClient = context.Device.System.LibHacHorizonManager.ApplicationClient;
  30. _baseFileSystemProxy = applicationClient.Fs.Impl.GetFileSystemProxyServiceObject();
  31. }
  32. [CommandHipc(1)]
  33. // SetCurrentProcess(u64, pid)
  34. public ResultCode SetCurrentProcess(ServiceCtx context)
  35. {
  36. return ResultCode.Success;
  37. }
  38. [CommandHipc(8)]
  39. // OpenFileSystemWithId(nn::fssrv::sf::FileSystemType filesystem_type, nn::ApplicationId tid, buffer<bytes<0x301>, 0x19, 0x301> path)
  40. // -> object<nn::fssrv::sf::IFileSystem> contentFs
  41. public ResultCode OpenFileSystemWithId(ServiceCtx context)
  42. {
  43. FileSystemType fileSystemType = (FileSystemType)context.RequestData.ReadInt32();
  44. ulong titleId = context.RequestData.ReadUInt64();
  45. string switchPath = ReadUtf8String(context);
  46. string fullPath = context.Device.FileSystem.SwitchPathToSystemPath(switchPath);
  47. if (!File.Exists(fullPath))
  48. {
  49. if (fullPath.Contains("."))
  50. {
  51. ResultCode result = FileSystemProxyHelper.OpenFileSystemFromInternalFile(context, fullPath, out FileSystemProxy.IFileSystem fileSystem);
  52. if (result == ResultCode.Success)
  53. {
  54. MakeObject(context, fileSystem);
  55. }
  56. return result;
  57. }
  58. return ResultCode.PathDoesNotExist;
  59. }
  60. FileStream fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
  61. string extension = System.IO.Path.GetExtension(fullPath);
  62. if (extension == ".nca")
  63. {
  64. ResultCode result = FileSystemProxyHelper.OpenNcaFs(context, fullPath, fileStream.AsStorage(), out FileSystemProxy.IFileSystem fileSystem);
  65. if (result == ResultCode.Success)
  66. {
  67. MakeObject(context, fileSystem);
  68. }
  69. return result;
  70. }
  71. else if (extension == ".nsp")
  72. {
  73. ResultCode result = FileSystemProxyHelper.OpenNsp(context, fullPath, out FileSystemProxy.IFileSystem fileSystem);
  74. if (result == ResultCode.Success)
  75. {
  76. MakeObject(context, fileSystem);
  77. }
  78. return result;
  79. }
  80. return ResultCode.InvalidInput;
  81. }
  82. [CommandHipc(11)]
  83. // OpenBisFileSystem(nn::fssrv::sf::Partition partitionID, buffer<bytes<0x301>, 0x19, 0x301>) -> object<nn::fssrv::sf::IFileSystem> Bis
  84. public ResultCode OpenBisFileSystem(ServiceCtx context)
  85. {
  86. BisPartitionId bisPartitionId = (BisPartitionId)context.RequestData.ReadInt32();
  87. ref readonly var path = ref FileSystemProxyHelper.GetFspPath(context);
  88. using var fileSystem = new SharedRef<IFileSystem>();
  89. Result result = _baseFileSystemProxy.Get.OpenBisFileSystem(ref fileSystem.Ref(), in path, bisPartitionId);
  90. if (result.IsFailure()) return (ResultCode)result.Value;
  91. MakeObject(context, new FileSystemProxy.IFileSystem(ref fileSystem.Ref()));
  92. return ResultCode.Success;
  93. }
  94. [CommandHipc(12)]
  95. // OpenBisStorage(u32 partitionId) -> object<nn::fssrv::sf::IStorage> bisStorage
  96. public ResultCode OpenBisStorage(ServiceCtx context)
  97. {
  98. BisPartitionId bisPartitionId = (BisPartitionId)context.RequestData.ReadInt32();
  99. using var storage = new SharedRef<IStorage>();
  100. Result result = _baseFileSystemProxy.Get.OpenBisStorage(ref storage.Ref(), bisPartitionId);
  101. if (result.IsFailure()) return (ResultCode)result.Value;
  102. MakeObject(context, new FileSystemProxy.IStorage(ref storage.Ref()));
  103. return ResultCode.Success;
  104. }
  105. [CommandHipc(13)]
  106. // InvalidateBisCache() -> ()
  107. public ResultCode InvalidateBisCache(ServiceCtx context)
  108. {
  109. return (ResultCode)_baseFileSystemProxy.Get.InvalidateBisCache().Value;
  110. }
  111. [CommandHipc(18)]
  112. // OpenSdCardFileSystem() -> object<nn::fssrv::sf::IFileSystem>
  113. public ResultCode OpenSdCardFileSystem(ServiceCtx context)
  114. {
  115. using var fileSystem = new SharedRef<IFileSystem>();
  116. Result result = _baseFileSystemProxy.Get.OpenSdCardFileSystem(ref fileSystem.Ref());
  117. if (result.IsFailure()) return (ResultCode)result.Value;
  118. MakeObject(context, new FileSystemProxy.IFileSystem(ref fileSystem.Ref()));
  119. return ResultCode.Success;
  120. }
  121. [CommandHipc(19)]
  122. // FormatSdCardFileSystem() -> ()
  123. public ResultCode FormatSdCardFileSystem(ServiceCtx context)
  124. {
  125. return (ResultCode)_baseFileSystemProxy.Get.FormatSdCardFileSystem().Value;
  126. }
  127. [CommandHipc(21)]
  128. // DeleteSaveDataFileSystem(u64 saveDataId) -> ()
  129. public ResultCode DeleteSaveDataFileSystem(ServiceCtx context)
  130. {
  131. ulong saveDataId = context.RequestData.ReadUInt64();
  132. return (ResultCode)_baseFileSystemProxy.Get.DeleteSaveDataFileSystem(saveDataId).Value;
  133. }
  134. [CommandHipc(22)]
  135. // CreateSaveDataFileSystem(nn::fs::SaveDataAttribute attribute, nn::fs::SaveDataCreationInfo creationInfo, nn::fs::SaveDataMetaInfo metaInfo) -> ()
  136. public ResultCode CreateSaveDataFileSystem(ServiceCtx context)
  137. {
  138. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  139. SaveDataCreationInfo creationInfo = context.RequestData.ReadStruct<SaveDataCreationInfo>();
  140. SaveDataMetaInfo metaInfo = context.RequestData.ReadStruct<SaveDataMetaInfo>();
  141. return (ResultCode)_baseFileSystemProxy.Get.CreateSaveDataFileSystem(in attribute, in creationInfo, in metaInfo).Value;
  142. }
  143. [CommandHipc(23)]
  144. // CreateSaveDataFileSystemBySystemSaveDataId(nn::fs::SaveDataAttribute attribute, nn::fs::SaveDataCreationInfo creationInfo) -> ()
  145. public ResultCode CreateSaveDataFileSystemBySystemSaveDataId(ServiceCtx context)
  146. {
  147. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  148. SaveDataCreationInfo creationInfo = context.RequestData.ReadStruct<SaveDataCreationInfo>();
  149. return (ResultCode)_baseFileSystemProxy.Get.CreateSaveDataFileSystemBySystemSaveDataId(in attribute, in creationInfo).Value;
  150. }
  151. [CommandHipc(24)]
  152. // RegisterSaveDataFileSystemAtomicDeletion(buffer<u64, 5> saveDataIds) -> ()
  153. public ResultCode RegisterSaveDataFileSystemAtomicDeletion(ServiceCtx context)
  154. {
  155. byte[] saveIdBuffer = new byte[context.Request.SendBuff[0].Size];
  156. context.Memory.Read(context.Request.SendBuff[0].Position, saveIdBuffer);
  157. return (ResultCode)_baseFileSystemProxy.Get.RegisterSaveDataFileSystemAtomicDeletion(new InBuffer(saveIdBuffer)).Value;
  158. }
  159. [CommandHipc(25)]
  160. // DeleteSaveDataFileSystemBySaveDataSpaceId(u8 spaceId, u64 saveDataId) -> ()
  161. public ResultCode DeleteSaveDataFileSystemBySaveDataSpaceId(ServiceCtx context)
  162. {
  163. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  164. ulong saveDataId = context.RequestData.ReadUInt64();
  165. return (ResultCode)_baseFileSystemProxy.Get.DeleteSaveDataFileSystemBySaveDataSpaceId(spaceId, saveDataId).Value;
  166. }
  167. [CommandHipc(26)]
  168. // FormatSdCardDryRun() -> ()
  169. public ResultCode FormatSdCardDryRun(ServiceCtx context)
  170. {
  171. return (ResultCode)_baseFileSystemProxy.Get.FormatSdCardDryRun().Value;
  172. }
  173. [CommandHipc(27)]
  174. // IsExFatSupported() -> (u8 isSupported)
  175. public ResultCode IsExFatSupported(ServiceCtx context)
  176. {
  177. Result result = _baseFileSystemProxy.Get.IsExFatSupported(out bool isSupported);
  178. if (result.IsFailure()) return (ResultCode)result.Value;
  179. context.ResponseData.Write(isSupported);
  180. return ResultCode.Success;
  181. }
  182. [CommandHipc(28)]
  183. // DeleteSaveDataFileSystemBySaveDataAttribute(u8 spaceId, nn::fs::SaveDataAttribute attribute) -> ()
  184. public ResultCode DeleteSaveDataFileSystemBySaveDataAttribute(ServiceCtx context)
  185. {
  186. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  187. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  188. return (ResultCode)_baseFileSystemProxy.Get.DeleteSaveDataFileSystemBySaveDataAttribute(spaceId, in attribute).Value;
  189. }
  190. [CommandHipc(30)]
  191. // OpenGameCardStorage(u32 handle, u32 partitionId) -> object<nn::fssrv::sf::IStorage>
  192. public ResultCode OpenGameCardStorage(ServiceCtx context)
  193. {
  194. GameCardHandle handle = new GameCardHandle(context.RequestData.ReadInt32());
  195. GameCardPartitionRaw partitionId = (GameCardPartitionRaw)context.RequestData.ReadInt32();
  196. using var storage = new SharedRef<IStorage>();
  197. Result result = _baseFileSystemProxy.Get.OpenGameCardStorage(ref storage.Ref(), handle, partitionId);
  198. if (result.IsFailure()) return (ResultCode)result.Value;
  199. MakeObject(context, new FileSystemProxy.IStorage(ref storage.Ref()));
  200. return ResultCode.Success;
  201. }
  202. [CommandHipc(31)]
  203. // OpenGameCardFileSystem(u32 handle, u32 partitionId) -> object<nn::fssrv::sf::IFileSystem>
  204. public ResultCode OpenGameCardFileSystem(ServiceCtx context)
  205. {
  206. GameCardHandle handle = new GameCardHandle(context.RequestData.ReadInt32());
  207. GameCardPartition partitionId = (GameCardPartition)context.RequestData.ReadInt32();
  208. using var fileSystem = new SharedRef<IFileSystem>();
  209. Result result = _baseFileSystemProxy.Get.OpenGameCardFileSystem(ref fileSystem.Ref(), handle, partitionId);
  210. if (result.IsFailure()) return (ResultCode)result.Value;
  211. MakeObject(context, new FileSystemProxy.IFileSystem(ref fileSystem.Ref()));
  212. return ResultCode.Success;
  213. }
  214. [CommandHipc(32)]
  215. // ExtendSaveDataFileSystem(u8 spaceId, u64 saveDataId, s64 dataSize, s64 journalSize) -> ()
  216. public ResultCode ExtendSaveDataFileSystem(ServiceCtx context)
  217. {
  218. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  219. ulong saveDataId = context.RequestData.ReadUInt64();
  220. long dataSize = context.RequestData.ReadInt64();
  221. long journalSize = context.RequestData.ReadInt64();
  222. return (ResultCode)_baseFileSystemProxy.Get.ExtendSaveDataFileSystem(spaceId, saveDataId, dataSize, journalSize).Value;
  223. }
  224. [CommandHipc(33)]
  225. // DeleteCacheStorage(u16 index) -> ()
  226. public ResultCode DeleteCacheStorage(ServiceCtx context)
  227. {
  228. ushort index = context.RequestData.ReadUInt16();
  229. return (ResultCode)_baseFileSystemProxy.Get.DeleteCacheStorage(index).Value;
  230. }
  231. [CommandHipc(34)]
  232. // GetCacheStorageSize(u16 index) -> (s64 dataSize, s64 journalSize)
  233. public ResultCode GetCacheStorageSize(ServiceCtx context)
  234. {
  235. ushort index = context.RequestData.ReadUInt16();
  236. Result result = _baseFileSystemProxy.Get.GetCacheStorageSize(out long dataSize, out long journalSize, index);
  237. if (result.IsFailure()) return (ResultCode)result.Value;
  238. context.ResponseData.Write(dataSize);
  239. context.ResponseData.Write(journalSize);
  240. return ResultCode.Success;
  241. }
  242. [CommandHipc(35)]
  243. // CreateSaveDataFileSystemWithHashSalt(nn::fs::SaveDataAttribute attribute, nn::fs::SaveDataCreationInfo creationInfo, nn::fs::SaveDataMetaInfo metaInfo nn::fs::HashSalt hashSalt) -> ()
  244. public ResultCode CreateSaveDataFileSystemWithHashSalt(ServiceCtx context)
  245. {
  246. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  247. SaveDataCreationInfo creationInfo = context.RequestData.ReadStruct<SaveDataCreationInfo>();
  248. SaveDataMetaInfo metaCreateInfo = context.RequestData.ReadStruct<SaveDataMetaInfo>();
  249. HashSalt hashSalt = context.RequestData.ReadStruct<HashSalt>();
  250. return (ResultCode)_baseFileSystemProxy.Get.CreateSaveDataFileSystemWithHashSalt(in attribute, in creationInfo, in metaCreateInfo, in hashSalt).Value;
  251. }
  252. [CommandHipc(51)]
  253. // OpenSaveDataFileSystem(u8 spaceId, nn::fs::SaveDataAttribute attribute) -> object<nn::fssrv::sf::IFileSystem> saveDataFs
  254. public ResultCode OpenSaveDataFileSystem(ServiceCtx context)
  255. {
  256. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  257. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  258. using var fileSystem = new SharedRef<IFileSystem>();
  259. Result result = _baseFileSystemProxy.Get.OpenSaveDataFileSystem(ref fileSystem.Ref(), spaceId, in attribute);
  260. if (result.IsFailure()) return (ResultCode)result.Value;
  261. MakeObject(context, new FileSystemProxy.IFileSystem(ref fileSystem.Ref()));
  262. return ResultCode.Success;
  263. }
  264. [CommandHipc(52)]
  265. // OpenSaveDataFileSystemBySystemSaveDataId(u8 spaceId, nn::fs::SaveDataAttribute attribute) -> object<nn::fssrv::sf::IFileSystem> systemSaveDataFs
  266. public ResultCode OpenSaveDataFileSystemBySystemSaveDataId(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.OpenSaveDataFileSystemBySystemSaveDataId(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. [CommandHipc(53)]
  277. // OpenReadOnlySaveDataFileSystem(u8 spaceId, nn::fs::SaveDataAttribute attribute) -> object<nn::fssrv::sf::IFileSystem>
  278. public ResultCode OpenReadOnlySaveDataFileSystem(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.OpenReadOnlySaveDataFileSystem(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. [CommandHipc(57)]
  289. // ReadSaveDataFileSystemExtraDataBySaveDataSpaceId(u8 spaceId, u64 saveDataId) -> (buffer<nn::fs::SaveDataExtraData, 6> extraData)
  290. public ResultCode ReadSaveDataFileSystemExtraDataBySaveDataSpaceId(ServiceCtx context)
  291. {
  292. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  293. ulong saveDataId = context.RequestData.ReadUInt64();
  294. byte[] extraDataBuffer = new byte[context.Request.ReceiveBuff[0].Size];
  295. context.Memory.Read(context.Request.ReceiveBuff[0].Position, extraDataBuffer);
  296. Result result = _baseFileSystemProxy.Get.ReadSaveDataFileSystemExtraDataBySaveDataSpaceId(new OutBuffer(extraDataBuffer), spaceId, saveDataId);
  297. if (result.IsFailure()) return (ResultCode)result.Value;
  298. context.Memory.Write(context.Request.ReceiveBuff[0].Position, extraDataBuffer);
  299. return ResultCode.Success;
  300. }
  301. [CommandHipc(58)]
  302. // ReadSaveDataFileSystemExtraData(u64 saveDataId) -> (buffer<nn::fs::SaveDataExtraData, 6> extraData)
  303. public ResultCode ReadSaveDataFileSystemExtraData(ServiceCtx context)
  304. {
  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.ReadSaveDataFileSystemExtraData(new OutBuffer(extraDataBuffer), 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. [CommandHipc(59)]
  314. // WriteSaveDataFileSystemExtraData(u8 spaceId, u64 saveDataId, buffer<nn::fs::SaveDataExtraData, 5> extraData) -> ()
  315. public ResultCode WriteSaveDataFileSystemExtraData(ServiceCtx context)
  316. {
  317. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  318. ulong saveDataId = context.RequestData.ReadUInt64();
  319. byte[] extraDataBuffer = new byte[context.Request.SendBuff[0].Size];
  320. context.Memory.Read(context.Request.SendBuff[0].Position, extraDataBuffer);
  321. return (ResultCode)_baseFileSystemProxy.Get.WriteSaveDataFileSystemExtraData(saveDataId, spaceId, new InBuffer(extraDataBuffer)).Value;
  322. }
  323. [CommandHipc(60)]
  324. // OpenSaveDataInfoReader() -> object<nn::fssrv::sf::ISaveDataInfoReader>
  325. public ResultCode OpenSaveDataInfoReader(ServiceCtx context)
  326. {
  327. using var infoReader = new SharedRef<LibHac.FsSrv.Sf.ISaveDataInfoReader>();
  328. Result result = _baseFileSystemProxy.Get.OpenSaveDataInfoReader(ref infoReader.Ref());
  329. if (result.IsFailure()) return (ResultCode)result.Value;
  330. MakeObject(context, new ISaveDataInfoReader(ref infoReader.Ref()));
  331. return ResultCode.Success;
  332. }
  333. [CommandHipc(61)]
  334. // OpenSaveDataInfoReaderBySaveDataSpaceId(u8 spaceId) -> object<nn::fssrv::sf::ISaveDataInfoReader>
  335. public ResultCode OpenSaveDataInfoReaderBySaveDataSpaceId(ServiceCtx context)
  336. {
  337. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadByte();
  338. using var infoReader = new SharedRef<LibHac.FsSrv.Sf.ISaveDataInfoReader>();
  339. Result result = _baseFileSystemProxy.Get.OpenSaveDataInfoReaderBySaveDataSpaceId(ref infoReader.Ref(), spaceId);
  340. if (result.IsFailure()) return (ResultCode)result.Value;
  341. MakeObject(context, new ISaveDataInfoReader(ref infoReader.Ref()));
  342. return ResultCode.Success;
  343. }
  344. [CommandHipc(62)]
  345. // OpenSaveDataInfoReaderOnlyCacheStorage() -> object<nn::fssrv::sf::ISaveDataInfoReader>
  346. public ResultCode OpenSaveDataInfoReaderOnlyCacheStorage(ServiceCtx context)
  347. {
  348. using var infoReader = new SharedRef<LibHac.FsSrv.Sf.ISaveDataInfoReader>();
  349. Result result = _baseFileSystemProxy.Get.OpenSaveDataInfoReaderOnlyCacheStorage(ref infoReader.Ref());
  350. if (result.IsFailure()) return (ResultCode)result.Value;
  351. MakeObject(context, new ISaveDataInfoReader(ref infoReader.Ref()));
  352. return ResultCode.Success;
  353. }
  354. [CommandHipc(64)]
  355. // OpenSaveDataInternalStorageFileSystem(u8 spaceId, u64 saveDataId) -> object<nn::fssrv::sf::ISaveDataInfoReader>
  356. public ResultCode OpenSaveDataInternalStorageFileSystem(ServiceCtx context)
  357. {
  358. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  359. ulong saveDataId = context.RequestData.ReadUInt64();
  360. using var fileSystem = new SharedRef<IFileSystem>();
  361. Result result = _baseFileSystemProxy.Get.OpenSaveDataInternalStorageFileSystem(ref fileSystem.Ref(), spaceId, saveDataId);
  362. if (result.IsFailure()) return (ResultCode)result.Value;
  363. MakeObject(context, new FileSystemProxy.IFileSystem(ref fileSystem.Ref()));
  364. return ResultCode.Success;
  365. }
  366. [CommandHipc(65)]
  367. // UpdateSaveDataMacForDebug(u8 spaceId, u64 saveDataId) -> ()
  368. public ResultCode UpdateSaveDataMacForDebug(ServiceCtx context)
  369. {
  370. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  371. ulong saveDataId = context.RequestData.ReadUInt64();
  372. return (ResultCode)_baseFileSystemProxy.Get.UpdateSaveDataMacForDebug(spaceId, saveDataId).Value;
  373. }
  374. [CommandHipc(66)]
  375. public ResultCode WriteSaveDataFileSystemExtraDataWithMask(ServiceCtx context)
  376. {
  377. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  378. ulong saveDataId = context.RequestData.ReadUInt64();
  379. byte[] extraDataBuffer = new byte[context.Request.SendBuff[0].Size];
  380. context.Memory.Read(context.Request.SendBuff[0].Position, extraDataBuffer);
  381. byte[] maskBuffer = new byte[context.Request.SendBuff[1].Size];
  382. context.Memory.Read(context.Request.SendBuff[1].Position, maskBuffer);
  383. return (ResultCode)_baseFileSystemProxy.Get.WriteSaveDataFileSystemExtraDataWithMask(saveDataId, spaceId, new InBuffer(extraDataBuffer), new InBuffer(maskBuffer)).Value;
  384. }
  385. [CommandHipc(67)]
  386. public ResultCode FindSaveDataWithFilter(ServiceCtx context)
  387. {
  388. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  389. SaveDataFilter filter = context.RequestData.ReadStruct<SaveDataFilter>();
  390. ulong bufferAddress = context.Request.ReceiveBuff[0].Position;
  391. ulong bufferLen = context.Request.ReceiveBuff[0].Size;
  392. using (var region = context.Memory.GetWritableRegion(bufferAddress, (int)bufferLen, true))
  393. {
  394. Result result = _baseFileSystemProxy.Get.FindSaveDataWithFilter(out long count, new OutBuffer(region.Memory.Span), spaceId, in filter);
  395. if (result.IsFailure()) return (ResultCode)result.Value;
  396. context.ResponseData.Write(count);
  397. }
  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(205)]
  605. // OpenDataStorageWithProgramIndex(u8 program_index) -> object<nn::fssrv::sf::IStorage>
  606. public ResultCode OpenDataStorageWithProgramIndex(ServiceCtx context)
  607. {
  608. byte programIndex = context.RequestData.ReadByte();
  609. if ((context.Device.Application.TitleId & 0xf) != programIndex)
  610. {
  611. throw new NotImplementedException($"Accessing storage from other programs is not supported (program index = {programIndex}).");
  612. }
  613. var storage = context.Device.FileSystem.RomFs.AsStorage(true);
  614. using var sharedStorage = new SharedRef<LibHac.Fs.IStorage>(storage);
  615. using var sfStorage = new SharedRef<IStorage>(new StorageInterfaceAdapter(ref sharedStorage.Ref()));
  616. MakeObject(context, new FileSystemProxy.IStorage(ref sfStorage.Ref()));
  617. return ResultCode.Success;
  618. }
  619. [CommandHipc(400)]
  620. // OpenDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage> dataStorage
  621. public ResultCode OpenDeviceOperator(ServiceCtx context)
  622. {
  623. using var deviceOperator = new SharedRef<LibHac.FsSrv.Sf.IDeviceOperator>();
  624. Result result = _baseFileSystemProxy.Get.OpenDeviceOperator(ref deviceOperator.Ref());
  625. if (result.IsFailure()) return (ResultCode)result.Value;
  626. MakeObject(context, new IDeviceOperator(ref deviceOperator.Ref()));
  627. return ResultCode.Success;
  628. }
  629. [CommandHipc(601)]
  630. public ResultCode QuerySaveDataTotalSize(ServiceCtx context)
  631. {
  632. long dataSize = context.RequestData.ReadInt64();
  633. long journalSize = context.RequestData.ReadInt64();
  634. Result result = _baseFileSystemProxy.Get.QuerySaveDataTotalSize(out long totalSize, dataSize, journalSize);
  635. if (result.IsFailure()) return (ResultCode)result.Value;
  636. context.ResponseData.Write(totalSize);
  637. return ResultCode.Success;
  638. }
  639. [CommandHipc(511)]
  640. public ResultCode NotifySystemDataUpdateEvent(ServiceCtx context)
  641. {
  642. return (ResultCode)_baseFileSystemProxy.Get.NotifySystemDataUpdateEvent().Value;
  643. }
  644. [CommandHipc(523)]
  645. public ResultCode SimulateDeviceDetectionEvent(ServiceCtx context)
  646. {
  647. bool signalEvent = context.RequestData.ReadBoolean();
  648. context.RequestData.BaseStream.Seek(3, SeekOrigin.Current);
  649. SdmmcPort port = context.RequestData.ReadStruct<SdmmcPort>();
  650. SimulatingDeviceDetectionMode mode = context.RequestData.ReadStruct<SimulatingDeviceDetectionMode>();
  651. return (ResultCode)_baseFileSystemProxy.Get.SimulateDeviceDetectionEvent(port, mode, signalEvent).Value;
  652. }
  653. [CommandHipc(602)]
  654. public ResultCode VerifySaveDataFileSystem(ServiceCtx context)
  655. {
  656. ulong saveDataId = context.RequestData.ReadUInt64();
  657. byte[] readBuffer = new byte[context.Request.ReceiveBuff[0].Size];
  658. context.Memory.Read(context.Request.ReceiveBuff[0].Position, readBuffer);
  659. return (ResultCode)_baseFileSystemProxy.Get.VerifySaveDataFileSystem(saveDataId, new OutBuffer(readBuffer)).Value;
  660. }
  661. [CommandHipc(603)]
  662. public ResultCode CorruptSaveDataFileSystem(ServiceCtx context)
  663. {
  664. ulong saveDataId = context.RequestData.ReadUInt64();
  665. return (ResultCode)_baseFileSystemProxy.Get.CorruptSaveDataFileSystem(saveDataId).Value;
  666. }
  667. [CommandHipc(604)]
  668. public ResultCode CreatePaddingFile(ServiceCtx context)
  669. {
  670. long size = context.RequestData.ReadInt64();
  671. return (ResultCode)_baseFileSystemProxy.Get.CreatePaddingFile(size).Value;
  672. }
  673. [CommandHipc(605)]
  674. public ResultCode DeleteAllPaddingFiles(ServiceCtx context)
  675. {
  676. return (ResultCode)_baseFileSystemProxy.Get.DeleteAllPaddingFiles().Value;
  677. }
  678. [CommandHipc(606)]
  679. public ResultCode GetRightsId(ServiceCtx context)
  680. {
  681. LibHac.Ncm.StorageId storageId = (LibHac.Ncm.StorageId)context.RequestData.ReadInt64();
  682. ProgramId programId = context.RequestData.ReadStruct<ProgramId>();
  683. Result result = _baseFileSystemProxy.Get.GetRightsId(out RightsId rightsId, programId, storageId);
  684. if (result.IsFailure()) return (ResultCode)result.Value;
  685. context.ResponseData.WriteStruct(rightsId);
  686. return ResultCode.Success;
  687. }
  688. [CommandHipc(607)]
  689. public ResultCode RegisterExternalKey(ServiceCtx context)
  690. {
  691. RightsId rightsId = context.RequestData.ReadStruct<RightsId>();
  692. AccessKey accessKey = context.RequestData.ReadStruct<AccessKey>();
  693. return (ResultCode)_baseFileSystemProxy.Get.RegisterExternalKey(in rightsId, in accessKey).Value;
  694. }
  695. [CommandHipc(608)]
  696. public ResultCode UnregisterAllExternalKey(ServiceCtx context)
  697. {
  698. return (ResultCode)_baseFileSystemProxy.Get.UnregisterAllExternalKey().Value;
  699. }
  700. [CommandHipc(609)]
  701. public ResultCode GetRightsIdByPath(ServiceCtx context)
  702. {
  703. ref readonly var path = ref FileSystemProxyHelper.GetFspPath(context);
  704. Result result = _baseFileSystemProxy.Get.GetRightsIdByPath(out RightsId rightsId, in path);
  705. if (result.IsFailure()) return (ResultCode)result.Value;
  706. context.ResponseData.WriteStruct(rightsId);
  707. return ResultCode.Success;
  708. }
  709. [CommandHipc(610)]
  710. public ResultCode GetRightsIdAndKeyGenerationByPath(ServiceCtx context)
  711. {
  712. ref readonly var path = ref FileSystemProxyHelper.GetFspPath(context);
  713. Result result = _baseFileSystemProxy.Get.GetRightsIdAndKeyGenerationByPath(out RightsId rightsId, out byte keyGeneration, in path);
  714. if (result.IsFailure()) return (ResultCode)result.Value;
  715. context.ResponseData.Write(keyGeneration);
  716. context.ResponseData.BaseStream.Seek(7, SeekOrigin.Current);
  717. context.ResponseData.WriteStruct(rightsId);
  718. return ResultCode.Success;
  719. }
  720. [CommandHipc(611)]
  721. public ResultCode SetCurrentPosixTimeWithTimeDifference(ServiceCtx context)
  722. {
  723. int timeDifference = context.RequestData.ReadInt32();
  724. context.RequestData.BaseStream.Seek(4, SeekOrigin.Current);
  725. long time = context.RequestData.ReadInt64();
  726. return (ResultCode)_baseFileSystemProxy.Get.SetCurrentPosixTimeWithTimeDifference(time, timeDifference).Value;
  727. }
  728. [CommandHipc(612)]
  729. public ResultCode GetFreeSpaceSizeForSaveData(ServiceCtx context)
  730. {
  731. SaveDataSpaceId spaceId = context.RequestData.ReadStruct<SaveDataSpaceId>();
  732. Result result = _baseFileSystemProxy.Get.GetFreeSpaceSizeForSaveData(out long freeSpaceSize, spaceId);
  733. if (result.IsFailure()) return (ResultCode)result.Value;
  734. context.ResponseData.Write(freeSpaceSize);
  735. return ResultCode.Success;
  736. }
  737. [CommandHipc(613)]
  738. public ResultCode VerifySaveDataFileSystemBySaveDataSpaceId(ServiceCtx context)
  739. {
  740. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  741. ulong saveDataId = context.RequestData.ReadUInt64();
  742. byte[] readBuffer = new byte[context.Request.ReceiveBuff[0].Size];
  743. context.Memory.Read(context.Request.ReceiveBuff[0].Position, readBuffer);
  744. return (ResultCode)_baseFileSystemProxy.Get.VerifySaveDataFileSystemBySaveDataSpaceId(spaceId, saveDataId, new OutBuffer(readBuffer)).Value;
  745. }
  746. [CommandHipc(614)]
  747. public ResultCode CorruptSaveDataFileSystemBySaveDataSpaceId(ServiceCtx context)
  748. {
  749. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  750. ulong saveDataId = context.RequestData.ReadUInt64();
  751. return (ResultCode)_baseFileSystemProxy.Get.CorruptSaveDataFileSystemBySaveDataSpaceId(spaceId, saveDataId).Value;
  752. }
  753. [CommandHipc(615)]
  754. public ResultCode QuerySaveDataInternalStorageTotalSize(ServiceCtx context)
  755. {
  756. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  757. ulong saveDataId = context.RequestData.ReadUInt64();
  758. Result result = _baseFileSystemProxy.Get.QuerySaveDataInternalStorageTotalSize(out long size, spaceId, saveDataId);
  759. if (result.IsFailure()) return (ResultCode)result.Value;
  760. context.ResponseData.Write(size);
  761. return ResultCode.Success;
  762. }
  763. [CommandHipc(616)]
  764. public ResultCode GetSaveDataCommitId(ServiceCtx context)
  765. {
  766. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  767. ulong saveDataId = context.RequestData.ReadUInt64();
  768. Result result = _baseFileSystemProxy.Get.GetSaveDataCommitId(out long commitId, spaceId, saveDataId);
  769. if (result.IsFailure()) return (ResultCode)result.Value;
  770. context.ResponseData.Write(commitId);
  771. return ResultCode.Success;
  772. }
  773. [CommandHipc(617)]
  774. public ResultCode UnregisterExternalKey(ServiceCtx context)
  775. {
  776. RightsId rightsId = context.RequestData.ReadStruct<RightsId>();
  777. return (ResultCode)_baseFileSystemProxy.Get.UnregisterExternalKey(in rightsId).Value;
  778. }
  779. [CommandHipc(620)]
  780. public ResultCode SetSdCardEncryptionSeed(ServiceCtx context)
  781. {
  782. EncryptionSeed encryptionSeed = context.RequestData.ReadStruct<EncryptionSeed>();
  783. return (ResultCode)_baseFileSystemProxy.Get.SetSdCardEncryptionSeed(in encryptionSeed).Value;
  784. }
  785. [CommandHipc(630)]
  786. // SetSdCardAccessibility(u8 isAccessible)
  787. public ResultCode SetSdCardAccessibility(ServiceCtx context)
  788. {
  789. bool isAccessible = context.RequestData.ReadBoolean();
  790. return (ResultCode)_baseFileSystemProxy.Get.SetSdCardAccessibility(isAccessible).Value;
  791. }
  792. [CommandHipc(631)]
  793. // IsSdCardAccessible() -> u8 isAccessible
  794. public ResultCode IsSdCardAccessible(ServiceCtx context)
  795. {
  796. Result result = _baseFileSystemProxy.Get.IsSdCardAccessible(out bool isAccessible);
  797. if (result.IsFailure()) return (ResultCode)result.Value;
  798. context.ResponseData.Write(isAccessible);
  799. return ResultCode.Success;
  800. }
  801. [CommandHipc(702)]
  802. public ResultCode IsAccessFailureDetected(ServiceCtx context)
  803. {
  804. ulong processId = context.RequestData.ReadUInt64();
  805. Result result = _baseFileSystemProxy.Get.IsAccessFailureDetected(out bool isDetected, processId);
  806. if (result.IsFailure()) return (ResultCode)result.Value;
  807. context.ResponseData.Write(isDetected);
  808. return ResultCode.Success;
  809. }
  810. [CommandHipc(710)]
  811. public ResultCode ResolveAccessFailure(ServiceCtx context)
  812. {
  813. ulong processId = context.RequestData.ReadUInt64();
  814. return (ResultCode)_baseFileSystemProxy.Get.ResolveAccessFailure(processId).Value;
  815. }
  816. [CommandHipc(720)]
  817. public ResultCode AbandonAccessFailure(ServiceCtx context)
  818. {
  819. ulong processId = context.RequestData.ReadUInt64();
  820. return (ResultCode)_baseFileSystemProxy.Get.AbandonAccessFailure(processId).Value;
  821. }
  822. [CommandHipc(800)]
  823. public ResultCode GetAndClearErrorInfo(ServiceCtx context)
  824. {
  825. Result result = _baseFileSystemProxy.Get.GetAndClearErrorInfo(out FileSystemProxyErrorInfo errorInfo);
  826. if (result.IsFailure()) return (ResultCode)result.Value;
  827. context.ResponseData.WriteStruct(errorInfo);
  828. return ResultCode.Success;
  829. }
  830. [CommandHipc(810)]
  831. public ResultCode RegisterProgramIndexMapInfo(ServiceCtx context)
  832. {
  833. int programCount = context.RequestData.ReadInt32();
  834. byte[] mapInfoBuffer = new byte[context.Request.SendBuff[0].Size];
  835. context.Memory.Read(context.Request.SendBuff[0].Position, mapInfoBuffer);
  836. return (ResultCode)_baseFileSystemProxy.Get.RegisterProgramIndexMapInfo(new InBuffer(mapInfoBuffer), programCount).Value;
  837. }
  838. [CommandHipc(1000)]
  839. public ResultCode SetBisRootForHost(ServiceCtx context)
  840. {
  841. BisPartitionId partitionId = (BisPartitionId)context.RequestData.ReadInt32();
  842. ref readonly var path = ref FileSystemProxyHelper.GetFspPath(context);
  843. return (ResultCode)_baseFileSystemProxy.Get.SetBisRootForHost(partitionId, in path).Value;
  844. }
  845. [CommandHipc(1001)]
  846. public ResultCode SetSaveDataSize(ServiceCtx context)
  847. {
  848. long dataSize = context.RequestData.ReadInt64();
  849. long journalSize = context.RequestData.ReadInt64();
  850. return (ResultCode)_baseFileSystemProxy.Get.SetSaveDataSize(dataSize, journalSize).Value;
  851. }
  852. [CommandHipc(1002)]
  853. public ResultCode SetSaveDataRootPath(ServiceCtx context)
  854. {
  855. ref readonly var path = ref FileSystemProxyHelper.GetFspPath(context);
  856. return (ResultCode)_baseFileSystemProxy.Get.SetSaveDataRootPath(in path).Value;
  857. }
  858. [CommandHipc(1003)]
  859. public ResultCode DisableAutoSaveDataCreation(ServiceCtx context)
  860. {
  861. return (ResultCode)_baseFileSystemProxy.Get.DisableAutoSaveDataCreation().Value;
  862. }
  863. [CommandHipc(1004)]
  864. // SetGlobalAccessLogMode(u32 mode)
  865. public ResultCode SetGlobalAccessLogMode(ServiceCtx context)
  866. {
  867. int mode = context.RequestData.ReadInt32();
  868. context.Device.System.GlobalAccessLogMode = mode;
  869. return ResultCode.Success;
  870. }
  871. [CommandHipc(1005)]
  872. // GetGlobalAccessLogMode() -> u32 logMode
  873. public ResultCode GetGlobalAccessLogMode(ServiceCtx context)
  874. {
  875. int mode = context.Device.System.GlobalAccessLogMode;
  876. context.ResponseData.Write(mode);
  877. return ResultCode.Success;
  878. }
  879. [CommandHipc(1006)]
  880. // OutputAccessLogToSdCard(buffer<bytes, 5> log_text)
  881. public ResultCode OutputAccessLogToSdCard(ServiceCtx context)
  882. {
  883. string message = ReadUtf8StringSend(context);
  884. // FS ends each line with a newline. Remove it because Ryujinx logging adds its own newline
  885. Logger.AccessLog?.PrintMsg(LogClass.ServiceFs, message.TrimEnd('\n'));
  886. return ResultCode.Success;
  887. }
  888. [CommandHipc(1007)]
  889. public ResultCode RegisterUpdatePartition(ServiceCtx context)
  890. {
  891. return (ResultCode)_baseFileSystemProxy.Get.RegisterUpdatePartition().Value;
  892. }
  893. [CommandHipc(1008)]
  894. public ResultCode OpenRegisteredUpdatePartition(ServiceCtx context)
  895. {
  896. using var fileSystem = new SharedRef<IFileSystem>();
  897. Result result = _baseFileSystemProxy.Get.OpenRegisteredUpdatePartition(ref fileSystem.Ref());
  898. if (result.IsFailure()) return (ResultCode)result.Value;
  899. MakeObject(context, new FileSystemProxy.IFileSystem(ref fileSystem.Ref()));
  900. return ResultCode.Success;
  901. }
  902. [CommandHipc(1009)]
  903. public ResultCode GetAndClearMemoryReportInfo(ServiceCtx context)
  904. {
  905. Result result = _baseFileSystemProxy.Get.GetAndClearMemoryReportInfo(out MemoryReportInfo reportInfo);
  906. if (result.IsFailure()) return (ResultCode)result.Value;
  907. context.ResponseData.WriteStruct(reportInfo);
  908. return ResultCode.Success;
  909. }
  910. [CommandHipc(1011)]
  911. public ResultCode GetProgramIndexForAccessLog(ServiceCtx context)
  912. {
  913. Result result = _baseFileSystemProxy.Get.GetProgramIndexForAccessLog(out int programIndex, out int programCount);
  914. if (result.IsFailure()) return (ResultCode)result.Value;
  915. context.ResponseData.Write(programIndex);
  916. context.ResponseData.Write(programCount);
  917. return ResultCode.Success;
  918. }
  919. [CommandHipc(1012)]
  920. public ResultCode GetFsStackUsage(ServiceCtx context)
  921. {
  922. FsStackUsageThreadType threadType = context.RequestData.ReadStruct<FsStackUsageThreadType>();
  923. Result result = _baseFileSystemProxy.Get.GetFsStackUsage(out uint usage, threadType);
  924. if (result.IsFailure()) return (ResultCode)result.Value;
  925. context.ResponseData.Write(usage);
  926. return ResultCode.Success;
  927. }
  928. [CommandHipc(1013)]
  929. public ResultCode UnsetSaveDataRootPath(ServiceCtx context)
  930. {
  931. return (ResultCode)_baseFileSystemProxy.Get.UnsetSaveDataRootPath().Value;
  932. }
  933. [CommandHipc(1014)]
  934. public ResultCode OutputMultiProgramTagAccessLog(ServiceCtx context)
  935. {
  936. return (ResultCode)_baseFileSystemProxy.Get.OutputMultiProgramTagAccessLog().Value;
  937. }
  938. [CommandHipc(1016)]
  939. public ResultCode FlushAccessLogOnSdCard(ServiceCtx context)
  940. {
  941. return (ResultCode)_baseFileSystemProxy.Get.FlushAccessLogOnSdCard().Value;
  942. }
  943. [CommandHipc(1017)]
  944. public ResultCode OutputApplicationInfoAccessLog(ServiceCtx context)
  945. {
  946. ApplicationInfo info = context.RequestData.ReadStruct<ApplicationInfo>();
  947. return (ResultCode)_baseFileSystemProxy.Get.OutputApplicationInfoAccessLog(in info).Value;
  948. }
  949. [CommandHipc(1100)]
  950. public ResultCode OverrideSaveDataTransferTokenSignVerificationKey(ServiceCtx context)
  951. {
  952. byte[] keyBuffer = new byte[context.Request.SendBuff[0].Size];
  953. context.Memory.Read(context.Request.SendBuff[0].Position, keyBuffer);
  954. return (ResultCode)_baseFileSystemProxy.Get.OverrideSaveDataTransferTokenSignVerificationKey(new InBuffer(keyBuffer)).Value;
  955. }
  956. [CommandHipc(1110)]
  957. public ResultCode CorruptSaveDataFileSystemByOffset(ServiceCtx context)
  958. {
  959. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  960. ulong saveDataId = context.RequestData.ReadUInt64();
  961. long offset = context.RequestData.ReadInt64();
  962. return (ResultCode)_baseFileSystemProxy.Get.CorruptSaveDataFileSystemByOffset(spaceId, saveDataId, offset).Value;
  963. }
  964. [CommandHipc(1200)] // 6.0.0+
  965. // OpenMultiCommitManager() -> object<nn::fssrv::sf::IMultiCommitManager>
  966. public ResultCode OpenMultiCommitManager(ServiceCtx context)
  967. {
  968. using var commitManager = new SharedRef<LibHac.FsSrv.Sf.IMultiCommitManager>();
  969. Result result = _baseFileSystemProxy.Get.OpenMultiCommitManager(ref commitManager.Ref());
  970. if (result.IsFailure()) return (ResultCode)result.Value;
  971. MakeObject(context, new IMultiCommitManager(ref commitManager.Ref()));
  972. return ResultCode.Success;
  973. }
  974. protected override void Dispose(bool isDisposing)
  975. {
  976. if (isDisposing)
  977. {
  978. _baseFileSystemProxy.Destroy();
  979. }
  980. }
  981. }
  982. }