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