IFileSystemProxy.cs 56 KB

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