IFileSystemProxy.cs 54 KB

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