IFileSystemProxy.cs 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  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. using GameCardHandle = System.UInt32;
  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 = context.RequestData.ReadUInt32();
  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 = context.RequestData.ReadUInt32();
  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(37)] // 14.0.0+
  254. // CreateSaveDataFileSystemWithCreationInfo2(buffer<nn::fs::SaveDataCreationInfo2, 25> creationInfo) -> ()
  255. public ResultCode CreateSaveDataFileSystemWithCreationInfo2(ServiceCtx context)
  256. {
  257. byte[] creationInfoBuffer = new byte[context.Request.SendBuff[0].Size];
  258. context.Memory.Read(context.Request.SendBuff[0].Position, creationInfoBuffer);
  259. ref readonly SaveDataCreationInfo2 creationInfo = ref SpanHelpers.AsReadOnlyStruct<SaveDataCreationInfo2>(creationInfoBuffer);
  260. return (ResultCode)_baseFileSystemProxy.Get.CreateSaveDataFileSystemWithCreationInfo2(in creationInfo).Value;
  261. }
  262. [CommandHipc(51)]
  263. // OpenSaveDataFileSystem(u8 spaceId, nn::fs::SaveDataAttribute attribute) -> object<nn::fssrv::sf::IFileSystem> saveDataFs
  264. public ResultCode OpenSaveDataFileSystem(ServiceCtx context)
  265. {
  266. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  267. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  268. using var fileSystem = new SharedRef<IFileSystem>();
  269. Result result = _baseFileSystemProxy.Get.OpenSaveDataFileSystem(ref fileSystem.Ref(), spaceId, in attribute);
  270. if (result.IsFailure()) return (ResultCode)result.Value;
  271. MakeObject(context, new FileSystemProxy.IFileSystem(ref fileSystem.Ref()));
  272. return ResultCode.Success;
  273. }
  274. [CommandHipc(52)]
  275. // OpenSaveDataFileSystemBySystemSaveDataId(u8 spaceId, nn::fs::SaveDataAttribute attribute) -> object<nn::fssrv::sf::IFileSystem> systemSaveDataFs
  276. public ResultCode OpenSaveDataFileSystemBySystemSaveDataId(ServiceCtx context)
  277. {
  278. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  279. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  280. using var fileSystem = new SharedRef<IFileSystem>();
  281. Result result = _baseFileSystemProxy.Get.OpenSaveDataFileSystemBySystemSaveDataId(ref fileSystem.Ref(), spaceId, in attribute);
  282. if (result.IsFailure()) return (ResultCode)result.Value;
  283. MakeObject(context, new FileSystemProxy.IFileSystem(ref fileSystem.Ref()));
  284. return ResultCode.Success;
  285. }
  286. [CommandHipc(53)]
  287. // OpenReadOnlySaveDataFileSystem(u8 spaceId, nn::fs::SaveDataAttribute attribute) -> object<nn::fssrv::sf::IFileSystem>
  288. public ResultCode OpenReadOnlySaveDataFileSystem(ServiceCtx context)
  289. {
  290. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  291. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  292. using var fileSystem = new SharedRef<IFileSystem>();
  293. Result result = _baseFileSystemProxy.Get.OpenReadOnlySaveDataFileSystem(ref fileSystem.Ref(), spaceId, in attribute);
  294. if (result.IsFailure()) return (ResultCode)result.Value;
  295. MakeObject(context, new FileSystemProxy.IFileSystem(ref fileSystem.Ref()));
  296. return ResultCode.Success;
  297. }
  298. [CommandHipc(57)]
  299. // ReadSaveDataFileSystemExtraDataBySaveDataSpaceId(u8 spaceId, u64 saveDataId) -> (buffer<nn::fs::SaveDataExtraData, 6> extraData)
  300. public ResultCode ReadSaveDataFileSystemExtraDataBySaveDataSpaceId(ServiceCtx context)
  301. {
  302. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  303. ulong saveDataId = context.RequestData.ReadUInt64();
  304. byte[] extraDataBuffer = new byte[context.Request.ReceiveBuff[0].Size];
  305. context.Memory.Read(context.Request.ReceiveBuff[0].Position, extraDataBuffer);
  306. Result result = _baseFileSystemProxy.Get.ReadSaveDataFileSystemExtraDataBySaveDataSpaceId(new OutBuffer(extraDataBuffer), spaceId, saveDataId);
  307. if (result.IsFailure()) return (ResultCode)result.Value;
  308. context.Memory.Write(context.Request.ReceiveBuff[0].Position, extraDataBuffer);
  309. return ResultCode.Success;
  310. }
  311. [CommandHipc(58)]
  312. // ReadSaveDataFileSystemExtraData(u64 saveDataId) -> (buffer<nn::fs::SaveDataExtraData, 6> extraData)
  313. public ResultCode ReadSaveDataFileSystemExtraData(ServiceCtx context)
  314. {
  315. ulong saveDataId = context.RequestData.ReadUInt64();
  316. byte[] extraDataBuffer = new byte[context.Request.ReceiveBuff[0].Size];
  317. context.Memory.Read(context.Request.ReceiveBuff[0].Position, extraDataBuffer);
  318. Result result = _baseFileSystemProxy.Get.ReadSaveDataFileSystemExtraData(new OutBuffer(extraDataBuffer), saveDataId);
  319. if (result.IsFailure()) return (ResultCode)result.Value;
  320. context.Memory.Write(context.Request.ReceiveBuff[0].Position, extraDataBuffer);
  321. return ResultCode.Success;
  322. }
  323. [CommandHipc(59)]
  324. // WriteSaveDataFileSystemExtraData(u8 spaceId, u64 saveDataId, buffer<nn::fs::SaveDataExtraData, 5> extraData) -> ()
  325. public ResultCode WriteSaveDataFileSystemExtraData(ServiceCtx context)
  326. {
  327. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  328. ulong saveDataId = context.RequestData.ReadUInt64();
  329. byte[] extraDataBuffer = new byte[context.Request.SendBuff[0].Size];
  330. context.Memory.Read(context.Request.SendBuff[0].Position, extraDataBuffer);
  331. return (ResultCode)_baseFileSystemProxy.Get.WriteSaveDataFileSystemExtraData(saveDataId, spaceId, new InBuffer(extraDataBuffer)).Value;
  332. }
  333. [CommandHipc(60)]
  334. // OpenSaveDataInfoReader() -> object<nn::fssrv::sf::ISaveDataInfoReader>
  335. public ResultCode OpenSaveDataInfoReader(ServiceCtx context)
  336. {
  337. using var infoReader = new SharedRef<LibHac.FsSrv.Sf.ISaveDataInfoReader>();
  338. Result result = _baseFileSystemProxy.Get.OpenSaveDataInfoReader(ref infoReader.Ref());
  339. if (result.IsFailure()) return (ResultCode)result.Value;
  340. MakeObject(context, new ISaveDataInfoReader(ref infoReader.Ref()));
  341. return ResultCode.Success;
  342. }
  343. [CommandHipc(61)]
  344. // OpenSaveDataInfoReaderBySaveDataSpaceId(u8 spaceId) -> object<nn::fssrv::sf::ISaveDataInfoReader>
  345. public ResultCode OpenSaveDataInfoReaderBySaveDataSpaceId(ServiceCtx context)
  346. {
  347. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadByte();
  348. using var infoReader = new SharedRef<LibHac.FsSrv.Sf.ISaveDataInfoReader>();
  349. Result result = _baseFileSystemProxy.Get.OpenSaveDataInfoReaderBySaveDataSpaceId(ref infoReader.Ref(), spaceId);
  350. if (result.IsFailure()) return (ResultCode)result.Value;
  351. MakeObject(context, new ISaveDataInfoReader(ref infoReader.Ref()));
  352. return ResultCode.Success;
  353. }
  354. [CommandHipc(62)]
  355. // OpenSaveDataInfoReaderOnlyCacheStorage() -> object<nn::fssrv::sf::ISaveDataInfoReader>
  356. public ResultCode OpenSaveDataInfoReaderOnlyCacheStorage(ServiceCtx context)
  357. {
  358. using var infoReader = new SharedRef<LibHac.FsSrv.Sf.ISaveDataInfoReader>();
  359. Result result = _baseFileSystemProxy.Get.OpenSaveDataInfoReaderOnlyCacheStorage(ref infoReader.Ref());
  360. if (result.IsFailure()) return (ResultCode)result.Value;
  361. MakeObject(context, new ISaveDataInfoReader(ref infoReader.Ref()));
  362. return ResultCode.Success;
  363. }
  364. [CommandHipc(64)]
  365. // OpenSaveDataInternalStorageFileSystem(u8 spaceId, u64 saveDataId) -> object<nn::fssrv::sf::ISaveDataInfoReader>
  366. public ResultCode OpenSaveDataInternalStorageFileSystem(ServiceCtx context)
  367. {
  368. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  369. ulong saveDataId = context.RequestData.ReadUInt64();
  370. using var fileSystem = new SharedRef<IFileSystem>();
  371. Result result = _baseFileSystemProxy.Get.OpenSaveDataInternalStorageFileSystem(ref fileSystem.Ref(), spaceId, saveDataId);
  372. if (result.IsFailure()) return (ResultCode)result.Value;
  373. MakeObject(context, new FileSystemProxy.IFileSystem(ref fileSystem.Ref()));
  374. return ResultCode.Success;
  375. }
  376. [CommandHipc(65)]
  377. // UpdateSaveDataMacForDebug(u8 spaceId, u64 saveDataId) -> ()
  378. public ResultCode UpdateSaveDataMacForDebug(ServiceCtx context)
  379. {
  380. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  381. ulong saveDataId = context.RequestData.ReadUInt64();
  382. return (ResultCode)_baseFileSystemProxy.Get.UpdateSaveDataMacForDebug(spaceId, saveDataId).Value;
  383. }
  384. [CommandHipc(66)]
  385. public ResultCode WriteSaveDataFileSystemExtraDataWithMask(ServiceCtx context)
  386. {
  387. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  388. ulong saveDataId = context.RequestData.ReadUInt64();
  389. byte[] extraDataBuffer = new byte[context.Request.SendBuff[0].Size];
  390. context.Memory.Read(context.Request.SendBuff[0].Position, extraDataBuffer);
  391. byte[] maskBuffer = new byte[context.Request.SendBuff[1].Size];
  392. context.Memory.Read(context.Request.SendBuff[1].Position, maskBuffer);
  393. return (ResultCode)_baseFileSystemProxy.Get.WriteSaveDataFileSystemExtraDataWithMask(saveDataId, spaceId, new InBuffer(extraDataBuffer), new InBuffer(maskBuffer)).Value;
  394. }
  395. [CommandHipc(67)]
  396. public ResultCode FindSaveDataWithFilter(ServiceCtx context)
  397. {
  398. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  399. SaveDataFilter filter = context.RequestData.ReadStruct<SaveDataFilter>();
  400. ulong bufferAddress = context.Request.ReceiveBuff[0].Position;
  401. ulong bufferLen = context.Request.ReceiveBuff[0].Size;
  402. using (var region = context.Memory.GetWritableRegion(bufferAddress, (int)bufferLen, true))
  403. {
  404. Result result = _baseFileSystemProxy.Get.FindSaveDataWithFilter(out long count, new OutBuffer(region.Memory.Span), spaceId, in filter);
  405. if (result.IsFailure()) return (ResultCode)result.Value;
  406. context.ResponseData.Write(count);
  407. }
  408. return ResultCode.Success;
  409. }
  410. [CommandHipc(68)]
  411. public ResultCode OpenSaveDataInfoReaderWithFilter(ServiceCtx context)
  412. {
  413. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  414. SaveDataFilter filter = context.RequestData.ReadStruct<SaveDataFilter>();
  415. using var infoReader = new SharedRef<LibHac.FsSrv.Sf.ISaveDataInfoReader>();
  416. Result result = _baseFileSystemProxy.Get.OpenSaveDataInfoReaderWithFilter(ref infoReader.Ref(), spaceId, in filter);
  417. if (result.IsFailure()) return (ResultCode)result.Value;
  418. MakeObject(context, new ISaveDataInfoReader(ref infoReader.Ref()));
  419. return ResultCode.Success;
  420. }
  421. [CommandHipc(69)]
  422. public ResultCode ReadSaveDataFileSystemExtraDataBySaveDataAttribute(ServiceCtx context)
  423. {
  424. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  425. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  426. byte[] outputBuffer = new byte[context.Request.ReceiveBuff[0].Size];
  427. context.Memory.Read(context.Request.ReceiveBuff[0].Position, outputBuffer);
  428. Result result = _baseFileSystemProxy.Get.ReadSaveDataFileSystemExtraDataBySaveDataAttribute(new OutBuffer(outputBuffer), spaceId, in attribute);
  429. if (result.IsFailure()) return (ResultCode)result.Value;
  430. context.Memory.Write(context.Request.ReceiveBuff[0].Position, outputBuffer);
  431. return ResultCode.Success;
  432. }
  433. [CommandHipc(70)]
  434. public ResultCode WriteSaveDataFileSystemExtraDataWithMaskBySaveDataAttribute(ServiceCtx context)
  435. {
  436. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  437. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  438. byte[] extraDataBuffer = new byte[context.Request.SendBuff[0].Size];
  439. context.Memory.Read(context.Request.SendBuff[0].Position, extraDataBuffer);
  440. byte[] maskBuffer = new byte[context.Request.SendBuff[1].Size];
  441. context.Memory.Read(context.Request.SendBuff[1].Position, maskBuffer);
  442. return (ResultCode)_baseFileSystemProxy.Get.WriteSaveDataFileSystemExtraDataWithMaskBySaveDataAttribute(in attribute, spaceId, new InBuffer(extraDataBuffer), new InBuffer(maskBuffer)).Value;
  443. }
  444. [CommandHipc(71)]
  445. public ResultCode ReadSaveDataFileSystemExtraDataWithMaskBySaveDataAttribute(ServiceCtx context)
  446. {
  447. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  448. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  449. byte[] maskBuffer = new byte[context.Request.SendBuff[0].Size];
  450. context.Memory.Read(context.Request.SendBuff[0].Position, maskBuffer);
  451. byte[] outputBuffer = new byte[context.Request.ReceiveBuff[0].Size];
  452. context.Memory.Read(context.Request.ReceiveBuff[0].Position, outputBuffer);
  453. Result result = _baseFileSystemProxy.Get.ReadSaveDataFileSystemExtraDataWithMaskBySaveDataAttribute(new OutBuffer(outputBuffer), spaceId, in attribute, new InBuffer(maskBuffer));
  454. if (result.IsFailure()) return (ResultCode)result.Value;
  455. context.Memory.Write(context.Request.ReceiveBuff[0].Position, outputBuffer);
  456. return ResultCode.Success;
  457. }
  458. [CommandHipc(80)]
  459. public ResultCode OpenSaveDataMetaFile(ServiceCtx context)
  460. {
  461. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt32();
  462. SaveDataMetaType metaType = (SaveDataMetaType)context.RequestData.ReadInt32();
  463. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  464. using var file = new SharedRef<LibHac.FsSrv.Sf.IFile>();
  465. Result result = _baseFileSystemProxy.Get.OpenSaveDataMetaFile(ref file.Ref(), spaceId, in attribute, metaType);
  466. if (result.IsFailure()) return (ResultCode)result.Value;
  467. MakeObject(context, new IFile(ref file.Ref()));
  468. return ResultCode.Success;
  469. }
  470. [CommandHipc(84)]
  471. public ResultCode ListAccessibleSaveDataOwnerId(ServiceCtx context)
  472. {
  473. int startIndex = context.RequestData.ReadInt32();
  474. int bufferCount = context.RequestData.ReadInt32();
  475. ProgramId programId = context.RequestData.ReadStruct<ProgramId>();
  476. byte[] outputBuffer = new byte[context.Request.ReceiveBuff[0].Size];
  477. context.Memory.Read(context.Request.ReceiveBuff[0].Position, outputBuffer);
  478. Result result = _baseFileSystemProxy.Get.ListAccessibleSaveDataOwnerId(out int readCount, new OutBuffer(outputBuffer), programId, startIndex, bufferCount);
  479. if (result.IsFailure()) return (ResultCode)result.Value;
  480. context.ResponseData.Write(readCount);
  481. return ResultCode.Success;
  482. }
  483. [CommandHipc(100)]
  484. public ResultCode OpenImageDirectoryFileSystem(ServiceCtx context)
  485. {
  486. ImageDirectoryId directoryId = (ImageDirectoryId)context.RequestData.ReadInt32();
  487. using var fileSystem = new SharedRef<IFileSystem>();
  488. Result result = _baseFileSystemProxy.Get.OpenImageDirectoryFileSystem(ref fileSystem.Ref(), directoryId);
  489. if (result.IsFailure()) return (ResultCode)result.Value;
  490. MakeObject(context, new FileSystemProxy.IFileSystem(ref fileSystem.Ref()));
  491. return ResultCode.Success;
  492. }
  493. [CommandHipc(101)]
  494. public ResultCode OpenBaseFileSystem(ServiceCtx context)
  495. {
  496. BaseFileSystemId fileSystemId = (BaseFileSystemId)context.RequestData.ReadInt32();
  497. using var fileSystem = new SharedRef<IFileSystem>();
  498. Result result = _baseFileSystemProxy.Get.OpenBaseFileSystem(ref fileSystem.Ref(), fileSystemId);
  499. if (result.IsFailure()) return (ResultCode)result.Value;
  500. MakeObject(context, new FileSystemProxy.IFileSystem(ref fileSystem.Ref()));
  501. return ResultCode.Success;
  502. }
  503. [CommandHipc(110)]
  504. public ResultCode OpenContentStorageFileSystem(ServiceCtx context)
  505. {
  506. ContentStorageId contentStorageId = (ContentStorageId)context.RequestData.ReadInt32();
  507. using var fileSystem = new SharedRef<IFileSystem>();
  508. Result result = _baseFileSystemProxy.Get.OpenContentStorageFileSystem(ref fileSystem.Ref(), contentStorageId);
  509. if (result.IsFailure()) return (ResultCode)result.Value;
  510. MakeObject(context, new FileSystemProxy.IFileSystem(ref fileSystem.Ref()));
  511. return ResultCode.Success;
  512. }
  513. [CommandHipc(120)]
  514. public ResultCode OpenCloudBackupWorkStorageFileSystem(ServiceCtx context)
  515. {
  516. CloudBackupWorkStorageId storageId = (CloudBackupWorkStorageId)context.RequestData.ReadInt32();
  517. using var fileSystem = new SharedRef<IFileSystem>();
  518. Result result = _baseFileSystemProxy.Get.OpenCloudBackupWorkStorageFileSystem(ref fileSystem.Ref(), storageId);
  519. if (result.IsFailure()) return (ResultCode)result.Value;
  520. MakeObject(context, new FileSystemProxy.IFileSystem(ref fileSystem.Ref()));
  521. return ResultCode.Success;
  522. }
  523. [CommandHipc(130)]
  524. public ResultCode OpenCustomStorageFileSystem(ServiceCtx context)
  525. {
  526. CustomStorageId customStorageId = (CustomStorageId)context.RequestData.ReadInt32();
  527. using var fileSystem = new SharedRef<IFileSystem>();
  528. Result result = _baseFileSystemProxy.Get.OpenCustomStorageFileSystem(ref fileSystem.Ref(), customStorageId);
  529. if (result.IsFailure()) return (ResultCode)result.Value;
  530. MakeObject(context, new FileSystemProxy.IFileSystem(ref fileSystem.Ref()));
  531. return ResultCode.Success;
  532. }
  533. [CommandHipc(200)]
  534. // OpenDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage> dataStorage
  535. public ResultCode OpenDataStorageByCurrentProcess(ServiceCtx context)
  536. {
  537. var storage = context.Device.FileSystem.RomFs.AsStorage(true);
  538. using var sharedStorage = new SharedRef<LibHac.Fs.IStorage>(storage);
  539. using var sfStorage = new SharedRef<IStorage>(new StorageInterfaceAdapter(ref sharedStorage.Ref()));
  540. MakeObject(context, new FileSystemProxy.IStorage(ref sfStorage.Ref()));
  541. return ResultCode.Success;
  542. }
  543. [CommandHipc(202)]
  544. // OpenDataStorageByDataId(u8 storageId, nn::ncm::DataId dataId) -> object<nn::fssrv::sf::IStorage> dataStorage
  545. public ResultCode OpenDataStorageByDataId(ServiceCtx context)
  546. {
  547. StorageId storageId = (StorageId)context.RequestData.ReadByte();
  548. byte[] padding = context.RequestData.ReadBytes(7);
  549. ulong titleId = context.RequestData.ReadUInt64();
  550. // We do a mitm here to find if the request is for an AOC.
  551. // This is because AOC can be distributed over multiple containers in the emulator.
  552. if (context.Device.System.ContentManager.GetAocDataStorage(titleId, out LibHac.Fs.IStorage aocStorage, context.Device.Configuration.FsIntegrityCheckLevel))
  553. {
  554. Logger.Info?.Print(LogClass.Loader, $"Opened AddOnContent Data TitleID={titleId:X16}");
  555. var storage = context.Device.FileSystem.ModLoader.ApplyRomFsMods(titleId, aocStorage);
  556. using var sharedStorage = new SharedRef<LibHac.Fs.IStorage>(storage);
  557. using var sfStorage = new SharedRef<IStorage>(new StorageInterfaceAdapter(ref sharedStorage.Ref()));
  558. MakeObject(context, new FileSystemProxy.IStorage(ref sfStorage.Ref()));
  559. return ResultCode.Success;
  560. }
  561. NcaContentType contentType = NcaContentType.Data;
  562. StorageId installedStorage = context.Device.System.ContentManager.GetInstalledStorage(titleId, contentType, storageId);
  563. if (installedStorage == StorageId.None)
  564. {
  565. contentType = NcaContentType.PublicData;
  566. installedStorage = context.Device.System.ContentManager.GetInstalledStorage(titleId, contentType, storageId);
  567. }
  568. if (installedStorage != StorageId.None)
  569. {
  570. string contentPath = context.Device.System.ContentManager.GetInstalledContentPath(titleId, storageId, contentType);
  571. string installPath = context.Device.FileSystem.SwitchPathToSystemPath(contentPath);
  572. if (!string.IsNullOrWhiteSpace(installPath))
  573. {
  574. string ncaPath = installPath;
  575. if (File.Exists(ncaPath))
  576. {
  577. try
  578. {
  579. LibHac.Fs.IStorage ncaStorage = new LocalStorage(ncaPath, FileAccess.Read, FileMode.Open);
  580. Nca nca = new Nca(context.Device.System.KeySet, ncaStorage);
  581. LibHac.Fs.IStorage romfsStorage = nca.OpenStorage(NcaSectionType.Data, context.Device.System.FsIntegrityCheckLevel);
  582. using var sharedStorage = new SharedRef<LibHac.Fs.IStorage>(romfsStorage);
  583. using var sfStorage = new SharedRef<IStorage>(new StorageInterfaceAdapter(ref sharedStorage.Ref()));
  584. MakeObject(context, new FileSystemProxy.IStorage(ref sfStorage.Ref()));
  585. }
  586. catch (HorizonResultException ex)
  587. {
  588. return (ResultCode)ex.ResultValue.Value;
  589. }
  590. return ResultCode.Success;
  591. }
  592. else
  593. {
  594. throw new FileNotFoundException($"No Nca found in Path `{ncaPath}`.");
  595. }
  596. }
  597. else
  598. {
  599. throw new DirectoryNotFoundException($"Path for title id {titleId:x16} on Storage {storageId} was not found in Path {installPath}.");
  600. }
  601. }
  602. throw new FileNotFoundException($"System archive with titleid {titleId:x16} was not found on Storage {storageId}. Found in {installedStorage}.");
  603. }
  604. [CommandHipc(203)]
  605. // OpenPatchDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage>
  606. public ResultCode OpenPatchDataStorageByCurrentProcess(ServiceCtx context)
  607. {
  608. var storage = context.Device.FileSystem.RomFs.AsStorage(true);
  609. using var sharedStorage = new SharedRef<LibHac.Fs.IStorage>(storage);
  610. using var sfStorage = new SharedRef<IStorage>(new StorageInterfaceAdapter(ref sharedStorage.Ref()));
  611. MakeObject(context, new FileSystemProxy.IStorage(ref sfStorage.Ref()));
  612. return ResultCode.Success;
  613. }
  614. [CommandHipc(205)]
  615. // OpenDataStorageWithProgramIndex(u8 program_index) -> object<nn::fssrv::sf::IStorage>
  616. public ResultCode OpenDataStorageWithProgramIndex(ServiceCtx context)
  617. {
  618. byte programIndex = context.RequestData.ReadByte();
  619. if ((context.Device.Application.TitleId & 0xf) != programIndex)
  620. {
  621. throw new NotImplementedException($"Accessing storage from other programs is not supported (program index = {programIndex}).");
  622. }
  623. var storage = context.Device.FileSystem.RomFs.AsStorage(true);
  624. using var sharedStorage = new SharedRef<LibHac.Fs.IStorage>(storage);
  625. using var sfStorage = new SharedRef<IStorage>(new StorageInterfaceAdapter(ref sharedStorage.Ref()));
  626. MakeObject(context, new FileSystemProxy.IStorage(ref sfStorage.Ref()));
  627. return ResultCode.Success;
  628. }
  629. [CommandHipc(400)]
  630. // OpenDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage> dataStorage
  631. public ResultCode OpenDeviceOperator(ServiceCtx context)
  632. {
  633. using var deviceOperator = new SharedRef<LibHac.FsSrv.Sf.IDeviceOperator>();
  634. Result result = _baseFileSystemProxy.Get.OpenDeviceOperator(ref deviceOperator.Ref());
  635. if (result.IsFailure()) return (ResultCode)result.Value;
  636. MakeObject(context, new IDeviceOperator(ref deviceOperator.Ref()));
  637. return ResultCode.Success;
  638. }
  639. [CommandHipc(601)]
  640. public ResultCode QuerySaveDataTotalSize(ServiceCtx context)
  641. {
  642. long dataSize = context.RequestData.ReadInt64();
  643. long journalSize = context.RequestData.ReadInt64();
  644. Result result = _baseFileSystemProxy.Get.QuerySaveDataTotalSize(out long totalSize, dataSize, journalSize);
  645. if (result.IsFailure()) return (ResultCode)result.Value;
  646. context.ResponseData.Write(totalSize);
  647. return ResultCode.Success;
  648. }
  649. [CommandHipc(511)]
  650. public ResultCode NotifySystemDataUpdateEvent(ServiceCtx context)
  651. {
  652. return (ResultCode)_baseFileSystemProxy.Get.NotifySystemDataUpdateEvent().Value;
  653. }
  654. [CommandHipc(523)]
  655. public ResultCode SimulateDeviceDetectionEvent(ServiceCtx context)
  656. {
  657. bool signalEvent = context.RequestData.ReadBoolean();
  658. context.RequestData.BaseStream.Seek(3, SeekOrigin.Current);
  659. SdmmcPort port = context.RequestData.ReadStruct<SdmmcPort>();
  660. SimulatingDeviceDetectionMode mode = context.RequestData.ReadStruct<SimulatingDeviceDetectionMode>();
  661. return (ResultCode)_baseFileSystemProxy.Get.SimulateDeviceDetectionEvent(port, mode, signalEvent).Value;
  662. }
  663. [CommandHipc(602)]
  664. public ResultCode VerifySaveDataFileSystem(ServiceCtx context)
  665. {
  666. ulong saveDataId = context.RequestData.ReadUInt64();
  667. byte[] readBuffer = new byte[context.Request.ReceiveBuff[0].Size];
  668. context.Memory.Read(context.Request.ReceiveBuff[0].Position, readBuffer);
  669. return (ResultCode)_baseFileSystemProxy.Get.VerifySaveDataFileSystem(saveDataId, new OutBuffer(readBuffer)).Value;
  670. }
  671. [CommandHipc(603)]
  672. public ResultCode CorruptSaveDataFileSystem(ServiceCtx context)
  673. {
  674. ulong saveDataId = context.RequestData.ReadUInt64();
  675. return (ResultCode)_baseFileSystemProxy.Get.CorruptSaveDataFileSystem(saveDataId).Value;
  676. }
  677. [CommandHipc(604)]
  678. public ResultCode CreatePaddingFile(ServiceCtx context)
  679. {
  680. long size = context.RequestData.ReadInt64();
  681. return (ResultCode)_baseFileSystemProxy.Get.CreatePaddingFile(size).Value;
  682. }
  683. [CommandHipc(605)]
  684. public ResultCode DeleteAllPaddingFiles(ServiceCtx context)
  685. {
  686. return (ResultCode)_baseFileSystemProxy.Get.DeleteAllPaddingFiles().Value;
  687. }
  688. [CommandHipc(606)]
  689. public ResultCode GetRightsId(ServiceCtx context)
  690. {
  691. StorageId storageId = (StorageId)context.RequestData.ReadInt64();
  692. ProgramId programId = context.RequestData.ReadStruct<ProgramId>();
  693. Result result = _baseFileSystemProxy.Get.GetRightsId(out RightsId rightsId, programId, storageId);
  694. if (result.IsFailure()) return (ResultCode)result.Value;
  695. context.ResponseData.WriteStruct(rightsId);
  696. return ResultCode.Success;
  697. }
  698. [CommandHipc(607)]
  699. public ResultCode RegisterExternalKey(ServiceCtx context)
  700. {
  701. RightsId rightsId = context.RequestData.ReadStruct<RightsId>();
  702. AccessKey accessKey = context.RequestData.ReadStruct<AccessKey>();
  703. return (ResultCode)_baseFileSystemProxy.Get.RegisterExternalKey(in rightsId, in accessKey).Value;
  704. }
  705. [CommandHipc(608)]
  706. public ResultCode UnregisterAllExternalKey(ServiceCtx context)
  707. {
  708. return (ResultCode)_baseFileSystemProxy.Get.UnregisterAllExternalKey().Value;
  709. }
  710. [CommandHipc(609)]
  711. public ResultCode GetRightsIdByPath(ServiceCtx context)
  712. {
  713. ref readonly var path = ref FileSystemProxyHelper.GetFspPath(context);
  714. Result result = _baseFileSystemProxy.Get.GetRightsIdByPath(out RightsId rightsId, in path);
  715. if (result.IsFailure()) return (ResultCode)result.Value;
  716. context.ResponseData.WriteStruct(rightsId);
  717. return ResultCode.Success;
  718. }
  719. [CommandHipc(610)]
  720. public ResultCode GetRightsIdAndKeyGenerationByPath(ServiceCtx context)
  721. {
  722. ref readonly var path = ref FileSystemProxyHelper.GetFspPath(context);
  723. Result result = _baseFileSystemProxy.Get.GetRightsIdAndKeyGenerationByPath(out RightsId rightsId, out byte keyGeneration, in path);
  724. if (result.IsFailure()) return (ResultCode)result.Value;
  725. context.ResponseData.Write(keyGeneration);
  726. context.ResponseData.BaseStream.Seek(7, SeekOrigin.Current);
  727. context.ResponseData.WriteStruct(rightsId);
  728. return ResultCode.Success;
  729. }
  730. [CommandHipc(611)]
  731. public ResultCode SetCurrentPosixTimeWithTimeDifference(ServiceCtx context)
  732. {
  733. int timeDifference = context.RequestData.ReadInt32();
  734. context.RequestData.BaseStream.Seek(4, SeekOrigin.Current);
  735. long time = context.RequestData.ReadInt64();
  736. return (ResultCode)_baseFileSystemProxy.Get.SetCurrentPosixTimeWithTimeDifference(time, timeDifference).Value;
  737. }
  738. [CommandHipc(612)]
  739. public ResultCode GetFreeSpaceSizeForSaveData(ServiceCtx context)
  740. {
  741. SaveDataSpaceId spaceId = context.RequestData.ReadStruct<SaveDataSpaceId>();
  742. Result result = _baseFileSystemProxy.Get.GetFreeSpaceSizeForSaveData(out long freeSpaceSize, spaceId);
  743. if (result.IsFailure()) return (ResultCode)result.Value;
  744. context.ResponseData.Write(freeSpaceSize);
  745. return ResultCode.Success;
  746. }
  747. [CommandHipc(613)]
  748. public ResultCode VerifySaveDataFileSystemBySaveDataSpaceId(ServiceCtx context)
  749. {
  750. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  751. ulong saveDataId = context.RequestData.ReadUInt64();
  752. byte[] readBuffer = new byte[context.Request.ReceiveBuff[0].Size];
  753. context.Memory.Read(context.Request.ReceiveBuff[0].Position, readBuffer);
  754. return (ResultCode)_baseFileSystemProxy.Get.VerifySaveDataFileSystemBySaveDataSpaceId(spaceId, saveDataId, new OutBuffer(readBuffer)).Value;
  755. }
  756. [CommandHipc(614)]
  757. public ResultCode CorruptSaveDataFileSystemBySaveDataSpaceId(ServiceCtx context)
  758. {
  759. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  760. ulong saveDataId = context.RequestData.ReadUInt64();
  761. return (ResultCode)_baseFileSystemProxy.Get.CorruptSaveDataFileSystemBySaveDataSpaceId(spaceId, saveDataId).Value;
  762. }
  763. [CommandHipc(615)]
  764. public ResultCode QuerySaveDataInternalStorageTotalSize(ServiceCtx context)
  765. {
  766. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  767. ulong saveDataId = context.RequestData.ReadUInt64();
  768. Result result = _baseFileSystemProxy.Get.QuerySaveDataInternalStorageTotalSize(out long size, spaceId, saveDataId);
  769. if (result.IsFailure()) return (ResultCode)result.Value;
  770. context.ResponseData.Write(size);
  771. return ResultCode.Success;
  772. }
  773. [CommandHipc(616)]
  774. public ResultCode GetSaveDataCommitId(ServiceCtx context)
  775. {
  776. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  777. ulong saveDataId = context.RequestData.ReadUInt64();
  778. Result result = _baseFileSystemProxy.Get.GetSaveDataCommitId(out long commitId, spaceId, saveDataId);
  779. if (result.IsFailure()) return (ResultCode)result.Value;
  780. context.ResponseData.Write(commitId);
  781. return ResultCode.Success;
  782. }
  783. [CommandHipc(617)]
  784. public ResultCode UnregisterExternalKey(ServiceCtx context)
  785. {
  786. RightsId rightsId = context.RequestData.ReadStruct<RightsId>();
  787. return (ResultCode)_baseFileSystemProxy.Get.UnregisterExternalKey(in rightsId).Value;
  788. }
  789. [CommandHipc(620)]
  790. public ResultCode SetSdCardEncryptionSeed(ServiceCtx context)
  791. {
  792. EncryptionSeed encryptionSeed = context.RequestData.ReadStruct<EncryptionSeed>();
  793. return (ResultCode)_baseFileSystemProxy.Get.SetSdCardEncryptionSeed(in encryptionSeed).Value;
  794. }
  795. [CommandHipc(630)]
  796. // SetSdCardAccessibility(u8 isAccessible)
  797. public ResultCode SetSdCardAccessibility(ServiceCtx context)
  798. {
  799. bool isAccessible = context.RequestData.ReadBoolean();
  800. return (ResultCode)_baseFileSystemProxy.Get.SetSdCardAccessibility(isAccessible).Value;
  801. }
  802. [CommandHipc(631)]
  803. // IsSdCardAccessible() -> u8 isAccessible
  804. public ResultCode IsSdCardAccessible(ServiceCtx context)
  805. {
  806. Result result = _baseFileSystemProxy.Get.IsSdCardAccessible(out bool isAccessible);
  807. if (result.IsFailure()) return (ResultCode)result.Value;
  808. context.ResponseData.Write(isAccessible);
  809. return ResultCode.Success;
  810. }
  811. [CommandHipc(702)]
  812. public ResultCode IsAccessFailureDetected(ServiceCtx context)
  813. {
  814. ulong processId = context.RequestData.ReadUInt64();
  815. Result result = _baseFileSystemProxy.Get.IsAccessFailureDetected(out bool isDetected, processId);
  816. if (result.IsFailure()) return (ResultCode)result.Value;
  817. context.ResponseData.Write(isDetected);
  818. return ResultCode.Success;
  819. }
  820. [CommandHipc(710)]
  821. public ResultCode ResolveAccessFailure(ServiceCtx context)
  822. {
  823. ulong processId = context.RequestData.ReadUInt64();
  824. return (ResultCode)_baseFileSystemProxy.Get.ResolveAccessFailure(processId).Value;
  825. }
  826. [CommandHipc(720)]
  827. public ResultCode AbandonAccessFailure(ServiceCtx context)
  828. {
  829. ulong processId = context.RequestData.ReadUInt64();
  830. return (ResultCode)_baseFileSystemProxy.Get.AbandonAccessFailure(processId).Value;
  831. }
  832. [CommandHipc(800)]
  833. public ResultCode GetAndClearErrorInfo(ServiceCtx context)
  834. {
  835. Result result = _baseFileSystemProxy.Get.GetAndClearErrorInfo(out FileSystemProxyErrorInfo errorInfo);
  836. if (result.IsFailure()) return (ResultCode)result.Value;
  837. context.ResponseData.WriteStruct(errorInfo);
  838. return ResultCode.Success;
  839. }
  840. [CommandHipc(810)]
  841. public ResultCode RegisterProgramIndexMapInfo(ServiceCtx context)
  842. {
  843. int programCount = context.RequestData.ReadInt32();
  844. byte[] mapInfoBuffer = new byte[context.Request.SendBuff[0].Size];
  845. context.Memory.Read(context.Request.SendBuff[0].Position, mapInfoBuffer);
  846. return (ResultCode)_baseFileSystemProxy.Get.RegisterProgramIndexMapInfo(new InBuffer(mapInfoBuffer), programCount).Value;
  847. }
  848. [CommandHipc(1000)]
  849. public ResultCode SetBisRootForHost(ServiceCtx context)
  850. {
  851. BisPartitionId partitionId = (BisPartitionId)context.RequestData.ReadInt32();
  852. ref readonly var path = ref FileSystemProxyHelper.GetFspPath(context);
  853. return (ResultCode)_baseFileSystemProxy.Get.SetBisRootForHost(partitionId, in path).Value;
  854. }
  855. [CommandHipc(1001)]
  856. public ResultCode SetSaveDataSize(ServiceCtx context)
  857. {
  858. long dataSize = context.RequestData.ReadInt64();
  859. long journalSize = context.RequestData.ReadInt64();
  860. return (ResultCode)_baseFileSystemProxy.Get.SetSaveDataSize(dataSize, journalSize).Value;
  861. }
  862. [CommandHipc(1002)]
  863. public ResultCode SetSaveDataRootPath(ServiceCtx context)
  864. {
  865. ref readonly var path = ref FileSystemProxyHelper.GetFspPath(context);
  866. return (ResultCode)_baseFileSystemProxy.Get.SetSaveDataRootPath(in path).Value;
  867. }
  868. [CommandHipc(1003)]
  869. public ResultCode DisableAutoSaveDataCreation(ServiceCtx context)
  870. {
  871. return (ResultCode)_baseFileSystemProxy.Get.DisableAutoSaveDataCreation().Value;
  872. }
  873. [CommandHipc(1004)]
  874. // SetGlobalAccessLogMode(u32 mode)
  875. public ResultCode SetGlobalAccessLogMode(ServiceCtx context)
  876. {
  877. int mode = context.RequestData.ReadInt32();
  878. context.Device.System.GlobalAccessLogMode = mode;
  879. return ResultCode.Success;
  880. }
  881. [CommandHipc(1005)]
  882. // GetGlobalAccessLogMode() -> u32 logMode
  883. public ResultCode GetGlobalAccessLogMode(ServiceCtx context)
  884. {
  885. int mode = context.Device.System.GlobalAccessLogMode;
  886. context.ResponseData.Write(mode);
  887. return ResultCode.Success;
  888. }
  889. [CommandHipc(1006)]
  890. // OutputAccessLogToSdCard(buffer<bytes, 5> log_text)
  891. public ResultCode OutputAccessLogToSdCard(ServiceCtx context)
  892. {
  893. string message = ReadUtf8StringSend(context);
  894. // FS ends each line with a newline. Remove it because Ryujinx logging adds its own newline
  895. Logger.AccessLog?.PrintMsg(LogClass.ServiceFs, message.TrimEnd('\n'));
  896. return ResultCode.Success;
  897. }
  898. [CommandHipc(1007)]
  899. public ResultCode RegisterUpdatePartition(ServiceCtx context)
  900. {
  901. return (ResultCode)_baseFileSystemProxy.Get.RegisterUpdatePartition().Value;
  902. }
  903. [CommandHipc(1008)]
  904. public ResultCode OpenRegisteredUpdatePartition(ServiceCtx context)
  905. {
  906. using var fileSystem = new SharedRef<IFileSystem>();
  907. Result result = _baseFileSystemProxy.Get.OpenRegisteredUpdatePartition(ref fileSystem.Ref());
  908. if (result.IsFailure()) return (ResultCode)result.Value;
  909. MakeObject(context, new FileSystemProxy.IFileSystem(ref fileSystem.Ref()));
  910. return ResultCode.Success;
  911. }
  912. [CommandHipc(1009)]
  913. public ResultCode GetAndClearMemoryReportInfo(ServiceCtx context)
  914. {
  915. Result result = _baseFileSystemProxy.Get.GetAndClearMemoryReportInfo(out MemoryReportInfo reportInfo);
  916. if (result.IsFailure()) return (ResultCode)result.Value;
  917. context.ResponseData.WriteStruct(reportInfo);
  918. return ResultCode.Success;
  919. }
  920. [CommandHipc(1011)]
  921. public ResultCode GetProgramIndexForAccessLog(ServiceCtx context)
  922. {
  923. Result result = _baseFileSystemProxy.Get.GetProgramIndexForAccessLog(out int programIndex, out int programCount);
  924. if (result.IsFailure()) return (ResultCode)result.Value;
  925. context.ResponseData.Write(programIndex);
  926. context.ResponseData.Write(programCount);
  927. return ResultCode.Success;
  928. }
  929. [CommandHipc(1012)]
  930. public ResultCode GetFsStackUsage(ServiceCtx context)
  931. {
  932. FsStackUsageThreadType threadType = context.RequestData.ReadStruct<FsStackUsageThreadType>();
  933. Result result = _baseFileSystemProxy.Get.GetFsStackUsage(out uint usage, threadType);
  934. if (result.IsFailure()) return (ResultCode)result.Value;
  935. context.ResponseData.Write(usage);
  936. return ResultCode.Success;
  937. }
  938. [CommandHipc(1013)]
  939. public ResultCode UnsetSaveDataRootPath(ServiceCtx context)
  940. {
  941. return (ResultCode)_baseFileSystemProxy.Get.UnsetSaveDataRootPath().Value;
  942. }
  943. [CommandHipc(1014)]
  944. public ResultCode OutputMultiProgramTagAccessLog(ServiceCtx context)
  945. {
  946. return (ResultCode)_baseFileSystemProxy.Get.OutputMultiProgramTagAccessLog().Value;
  947. }
  948. [CommandHipc(1016)]
  949. public ResultCode FlushAccessLogOnSdCard(ServiceCtx context)
  950. {
  951. return (ResultCode)_baseFileSystemProxy.Get.FlushAccessLogOnSdCard().Value;
  952. }
  953. [CommandHipc(1017)]
  954. public ResultCode OutputApplicationInfoAccessLog(ServiceCtx context)
  955. {
  956. ApplicationInfo info = context.RequestData.ReadStruct<ApplicationInfo>();
  957. return (ResultCode)_baseFileSystemProxy.Get.OutputApplicationInfoAccessLog(in info).Value;
  958. }
  959. [CommandHipc(1100)]
  960. public ResultCode OverrideSaveDataTransferTokenSignVerificationKey(ServiceCtx context)
  961. {
  962. byte[] keyBuffer = new byte[context.Request.SendBuff[0].Size];
  963. context.Memory.Read(context.Request.SendBuff[0].Position, keyBuffer);
  964. return (ResultCode)_baseFileSystemProxy.Get.OverrideSaveDataTransferTokenSignVerificationKey(new InBuffer(keyBuffer)).Value;
  965. }
  966. [CommandHipc(1110)]
  967. public ResultCode CorruptSaveDataFileSystemByOffset(ServiceCtx context)
  968. {
  969. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  970. ulong saveDataId = context.RequestData.ReadUInt64();
  971. long offset = context.RequestData.ReadInt64();
  972. return (ResultCode)_baseFileSystemProxy.Get.CorruptSaveDataFileSystemByOffset(spaceId, saveDataId, offset).Value;
  973. }
  974. [CommandHipc(1200)] // 6.0.0+
  975. // OpenMultiCommitManager() -> object<nn::fssrv::sf::IMultiCommitManager>
  976. public ResultCode OpenMultiCommitManager(ServiceCtx context)
  977. {
  978. using var commitManager = new SharedRef<LibHac.FsSrv.Sf.IMultiCommitManager>();
  979. Result result = _baseFileSystemProxy.Get.OpenMultiCommitManager(ref commitManager.Ref());
  980. if (result.IsFailure()) return (ResultCode)result.Value;
  981. MakeObject(context, new IMultiCommitManager(ref commitManager.Ref()));
  982. return ResultCode.Success;
  983. }
  984. protected override void Dispose(bool isDisposing)
  985. {
  986. if (isDisposing)
  987. {
  988. _baseFileSystemProxy.Destroy();
  989. }
  990. }
  991. }
  992. }