IFileSystemProxy.cs 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  1. using LibHac;
  2. using LibHac.Common;
  3. using LibHac.Fs;
  4. using LibHac.Fs.Shim;
  5. using LibHac.FsSrv;
  6. using LibHac.FsSrv.Impl;
  7. using LibHac.FsSystem;
  8. using LibHac.FsSystem.NcaUtils;
  9. using LibHac.Ncm;
  10. using LibHac.Sf;
  11. using LibHac.Spl;
  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. using StorageId = Ryujinx.HLE.FileSystem.StorageId;
  21. namespace Ryujinx.HLE.HOS.Services.Fs
  22. {
  23. [Service("fsp-srv")]
  24. class IFileSystemProxy : DisposableIpcService
  25. {
  26. private SharedRef<LibHac.FsSrv.Sf.IFileSystemProxy> _baseFileSystemProxy;
  27. public IFileSystemProxy(ServiceCtx context)
  28. {
  29. var applicationClient = context.Device.System.LibHacHorizonManager.ApplicationClient;
  30. _baseFileSystemProxy = applicationClient.Fs.Impl.GetFileSystemProxyServiceObject();
  31. }
  32. [CommandHipc(1)]
  33. // SetCurrentProcess(u64, pid)
  34. public ResultCode SetCurrentProcess(ServiceCtx context)
  35. {
  36. return ResultCode.Success;
  37. }
  38. [CommandHipc(8)]
  39. // OpenFileSystemWithId(nn::fssrv::sf::FileSystemType filesystem_type, nn::ApplicationId tid, buffer<bytes<0x301>, 0x19, 0x301> path)
  40. // -> object<nn::fssrv::sf::IFileSystem> contentFs
  41. public ResultCode OpenFileSystemWithId(ServiceCtx context)
  42. {
  43. FileSystemType fileSystemType = (FileSystemType)context.RequestData.ReadInt32();
  44. ulong titleId = context.RequestData.ReadUInt64();
  45. string switchPath = ReadUtf8String(context);
  46. string fullPath = context.Device.FileSystem.SwitchPathToSystemPath(switchPath);
  47. if (!File.Exists(fullPath))
  48. {
  49. if (fullPath.Contains("."))
  50. {
  51. ResultCode result = FileSystemProxyHelper.OpenFileSystemFromInternalFile(context, fullPath, out FileSystemProxy.IFileSystem fileSystem);
  52. if (result == ResultCode.Success)
  53. {
  54. MakeObject(context, fileSystem);
  55. }
  56. return result;
  57. }
  58. return ResultCode.PathDoesNotExist;
  59. }
  60. FileStream fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
  61. string extension = System.IO.Path.GetExtension(fullPath);
  62. if (extension == ".nca")
  63. {
  64. ResultCode result = FileSystemProxyHelper.OpenNcaFs(context, fullPath, fileStream.AsStorage(), out FileSystemProxy.IFileSystem fileSystem);
  65. if (result == ResultCode.Success)
  66. {
  67. MakeObject(context, fileSystem);
  68. }
  69. return result;
  70. }
  71. else if (extension == ".nsp")
  72. {
  73. ResultCode result = FileSystemProxyHelper.OpenNsp(context, fullPath, out FileSystemProxy.IFileSystem fileSystem);
  74. if (result == ResultCode.Success)
  75. {
  76. MakeObject(context, fileSystem);
  77. }
  78. return result;
  79. }
  80. return ResultCode.InvalidInput;
  81. }
  82. [CommandHipc(11)]
  83. // OpenBisFileSystem(nn::fssrv::sf::Partition partitionID, buffer<bytes<0x301>, 0x19, 0x301>) -> object<nn::fssrv::sf::IFileSystem> Bis
  84. public ResultCode OpenBisFileSystem(ServiceCtx context)
  85. {
  86. BisPartitionId bisPartitionId = (BisPartitionId)context.RequestData.ReadInt32();
  87. ref readonly var path = ref FileSystemProxyHelper.GetFspPath(context);
  88. using var fileSystem = new SharedRef<IFileSystem>();
  89. Result result = _baseFileSystemProxy.Get.OpenBisFileSystem(ref fileSystem.Ref(), in path, bisPartitionId);
  90. if (result.IsFailure()) return (ResultCode)result.Value;
  91. MakeObject(context, new FileSystemProxy.IFileSystem(ref fileSystem.Ref()));
  92. return ResultCode.Success;
  93. }
  94. [CommandHipc(12)]
  95. // OpenBisStorage(u32 partitionId) -> object<nn::fssrv::sf::IStorage> bisStorage
  96. public ResultCode OpenBisStorage(ServiceCtx context)
  97. {
  98. BisPartitionId bisPartitionId = (BisPartitionId)context.RequestData.ReadInt32();
  99. using var storage = new SharedRef<IStorage>();
  100. Result result = _baseFileSystemProxy.Get.OpenBisStorage(ref storage.Ref(), bisPartitionId);
  101. if (result.IsFailure()) return (ResultCode)result.Value;
  102. MakeObject(context, new FileSystemProxy.IStorage(ref storage.Ref()));
  103. return ResultCode.Success;
  104. }
  105. [CommandHipc(13)]
  106. // InvalidateBisCache() -> ()
  107. public ResultCode InvalidateBisCache(ServiceCtx context)
  108. {
  109. return (ResultCode)_baseFileSystemProxy.Get.InvalidateBisCache().Value;
  110. }
  111. [CommandHipc(18)]
  112. // OpenSdCardFileSystem() -> object<nn::fssrv::sf::IFileSystem>
  113. public ResultCode OpenSdCardFileSystem(ServiceCtx context)
  114. {
  115. using var fileSystem = new SharedRef<IFileSystem>();
  116. Result result = _baseFileSystemProxy.Get.OpenSdCardFileSystem(ref fileSystem.Ref());
  117. if (result.IsFailure()) return (ResultCode)result.Value;
  118. MakeObject(context, new FileSystemProxy.IFileSystem(ref fileSystem.Ref()));
  119. return ResultCode.Success;
  120. }
  121. [CommandHipc(19)]
  122. // FormatSdCardFileSystem() -> ()
  123. public ResultCode FormatSdCardFileSystem(ServiceCtx context)
  124. {
  125. return (ResultCode)_baseFileSystemProxy.Get.FormatSdCardFileSystem().Value;
  126. }
  127. [CommandHipc(21)]
  128. // DeleteSaveDataFileSystem(u64 saveDataId) -> ()
  129. public ResultCode DeleteSaveDataFileSystem(ServiceCtx context)
  130. {
  131. ulong saveDataId = context.RequestData.ReadUInt64();
  132. return (ResultCode)_baseFileSystemProxy.Get.DeleteSaveDataFileSystem(saveDataId).Value;
  133. }
  134. [CommandHipc(22)]
  135. // CreateSaveDataFileSystem(nn::fs::SaveDataAttribute attribute, nn::fs::SaveDataCreationInfo creationInfo, nn::fs::SaveDataMetaInfo metaInfo) -> ()
  136. public ResultCode CreateSaveDataFileSystem(ServiceCtx context)
  137. {
  138. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  139. SaveDataCreationInfo creationInfo = context.RequestData.ReadStruct<SaveDataCreationInfo>();
  140. SaveDataMetaInfo metaInfo = context.RequestData.ReadStruct<SaveDataMetaInfo>();
  141. return (ResultCode)_baseFileSystemProxy.Get.CreateSaveDataFileSystem(in attribute, in creationInfo, in metaInfo).Value;
  142. }
  143. [CommandHipc(23)]
  144. // CreateSaveDataFileSystemBySystemSaveDataId(nn::fs::SaveDataAttribute attribute, nn::fs::SaveDataCreationInfo creationInfo) -> ()
  145. public ResultCode CreateSaveDataFileSystemBySystemSaveDataId(ServiceCtx context)
  146. {
  147. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  148. SaveDataCreationInfo creationInfo = context.RequestData.ReadStruct<SaveDataCreationInfo>();
  149. return (ResultCode)_baseFileSystemProxy.Get.CreateSaveDataFileSystemBySystemSaveDataId(in attribute, in creationInfo).Value;
  150. }
  151. [CommandHipc(24)]
  152. // RegisterSaveDataFileSystemAtomicDeletion(buffer<u64, 5> saveDataIds) -> ()
  153. public ResultCode RegisterSaveDataFileSystemAtomicDeletion(ServiceCtx context)
  154. {
  155. byte[] saveIdBuffer = new byte[context.Request.SendBuff[0].Size];
  156. context.Memory.Read(context.Request.SendBuff[0].Position, saveIdBuffer);
  157. return (ResultCode)_baseFileSystemProxy.Get.RegisterSaveDataFileSystemAtomicDeletion(new InBuffer(saveIdBuffer)).Value;
  158. }
  159. [CommandHipc(25)]
  160. // DeleteSaveDataFileSystemBySaveDataSpaceId(u8 spaceId, u64 saveDataId) -> ()
  161. public ResultCode DeleteSaveDataFileSystemBySaveDataSpaceId(ServiceCtx context)
  162. {
  163. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  164. ulong saveDataId = context.RequestData.ReadUInt64();
  165. return (ResultCode)_baseFileSystemProxy.Get.DeleteSaveDataFileSystemBySaveDataSpaceId(spaceId, saveDataId).Value;
  166. }
  167. [CommandHipc(26)]
  168. // FormatSdCardDryRun() -> ()
  169. public ResultCode FormatSdCardDryRun(ServiceCtx context)
  170. {
  171. return (ResultCode)_baseFileSystemProxy.Get.FormatSdCardDryRun().Value;
  172. }
  173. [CommandHipc(27)]
  174. // IsExFatSupported() -> (u8 isSupported)
  175. public ResultCode IsExFatSupported(ServiceCtx context)
  176. {
  177. Result result = _baseFileSystemProxy.Get.IsExFatSupported(out bool isSupported);
  178. if (result.IsFailure()) return (ResultCode)result.Value;
  179. context.ResponseData.Write(isSupported);
  180. return ResultCode.Success;
  181. }
  182. [CommandHipc(28)]
  183. // DeleteSaveDataFileSystemBySaveDataAttribute(u8 spaceId, nn::fs::SaveDataAttribute attribute) -> ()
  184. public ResultCode DeleteSaveDataFileSystemBySaveDataAttribute(ServiceCtx context)
  185. {
  186. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  187. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  188. return (ResultCode)_baseFileSystemProxy.Get.DeleteSaveDataFileSystemBySaveDataAttribute(spaceId, in attribute).Value;
  189. }
  190. [CommandHipc(30)]
  191. // OpenGameCardStorage(u32 handle, u32 partitionId) -> object<nn::fssrv::sf::IStorage>
  192. public ResultCode OpenGameCardStorage(ServiceCtx context)
  193. {
  194. GameCardHandle handle = new GameCardHandle(context.RequestData.ReadInt32());
  195. GameCardPartitionRaw partitionId = (GameCardPartitionRaw)context.RequestData.ReadInt32();
  196. using var storage = new SharedRef<IStorage>();
  197. Result result = _baseFileSystemProxy.Get.OpenGameCardStorage(ref storage.Ref(), handle, partitionId);
  198. if (result.IsFailure()) return (ResultCode)result.Value;
  199. MakeObject(context, new FileSystemProxy.IStorage(ref storage.Ref()));
  200. return ResultCode.Success;
  201. }
  202. [CommandHipc(31)]
  203. // OpenGameCardFileSystem(u32 handle, u32 partitionId) -> object<nn::fssrv::sf::IFileSystem>
  204. public ResultCode OpenGameCardFileSystem(ServiceCtx context)
  205. {
  206. GameCardHandle handle = new GameCardHandle(context.RequestData.ReadInt32());
  207. GameCardPartition partitionId = (GameCardPartition)context.RequestData.ReadInt32();
  208. using var fileSystem = new SharedRef<IFileSystem>();
  209. Result result = _baseFileSystemProxy.Get.OpenGameCardFileSystem(ref fileSystem.Ref(), handle, partitionId);
  210. if (result.IsFailure()) return (ResultCode)result.Value;
  211. MakeObject(context, new FileSystemProxy.IFileSystem(ref fileSystem.Ref()));
  212. return ResultCode.Success;
  213. }
  214. [CommandHipc(32)]
  215. // ExtendSaveDataFileSystem(u8 spaceId, u64 saveDataId, s64 dataSize, s64 journalSize) -> ()
  216. public ResultCode ExtendSaveDataFileSystem(ServiceCtx context)
  217. {
  218. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  219. ulong saveDataId = context.RequestData.ReadUInt64();
  220. long dataSize = context.RequestData.ReadInt64();
  221. long journalSize = context.RequestData.ReadInt64();
  222. return (ResultCode)_baseFileSystemProxy.Get.ExtendSaveDataFileSystem(spaceId, saveDataId, dataSize, journalSize).Value;
  223. }
  224. [CommandHipc(33)]
  225. // DeleteCacheStorage(u16 index) -> ()
  226. public ResultCode DeleteCacheStorage(ServiceCtx context)
  227. {
  228. ushort index = context.RequestData.ReadUInt16();
  229. return (ResultCode)_baseFileSystemProxy.Get.DeleteCacheStorage(index).Value;
  230. }
  231. [CommandHipc(34)]
  232. // GetCacheStorageSize(u16 index) -> (s64 dataSize, s64 journalSize)
  233. public ResultCode GetCacheStorageSize(ServiceCtx context)
  234. {
  235. ushort index = context.RequestData.ReadUInt16();
  236. Result result = _baseFileSystemProxy.Get.GetCacheStorageSize(out long dataSize, out long journalSize, index);
  237. if (result.IsFailure()) return (ResultCode)result.Value;
  238. context.ResponseData.Write(dataSize);
  239. context.ResponseData.Write(journalSize);
  240. return ResultCode.Success;
  241. }
  242. [CommandHipc(35)]
  243. // CreateSaveDataFileSystemWithHashSalt(nn::fs::SaveDataAttribute attribute, nn::fs::SaveDataCreationInfo creationInfo, nn::fs::SaveDataMetaInfo metaInfo nn::fs::HashSalt hashSalt) -> ()
  244. public ResultCode CreateSaveDataFileSystemWithHashSalt(ServiceCtx context)
  245. {
  246. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  247. SaveDataCreationInfo creationInfo = context.RequestData.ReadStruct<SaveDataCreationInfo>();
  248. SaveDataMetaInfo metaCreateInfo = context.RequestData.ReadStruct<SaveDataMetaInfo>();
  249. HashSalt hashSalt = context.RequestData.ReadStruct<HashSalt>();
  250. return (ResultCode)_baseFileSystemProxy.Get.CreateSaveDataFileSystemWithHashSalt(in attribute, in creationInfo, in metaCreateInfo, in hashSalt).Value;
  251. }
  252. [CommandHipc(51)]
  253. // OpenSaveDataFileSystem(u8 spaceId, nn::fs::SaveDataAttribute attribute) -> object<nn::fssrv::sf::IFileSystem> saveDataFs
  254. public ResultCode OpenSaveDataFileSystem(ServiceCtx context)
  255. {
  256. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  257. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  258. using var fileSystem = new SharedRef<IFileSystem>();
  259. Result result = _baseFileSystemProxy.Get.OpenSaveDataFileSystem(ref fileSystem.Ref(), spaceId, in attribute);
  260. if (result.IsFailure()) return (ResultCode)result.Value;
  261. MakeObject(context, new FileSystemProxy.IFileSystem(ref fileSystem.Ref()));
  262. return ResultCode.Success;
  263. }
  264. [CommandHipc(52)]
  265. // OpenSaveDataFileSystemBySystemSaveDataId(u8 spaceId, nn::fs::SaveDataAttribute attribute) -> object<nn::fssrv::sf::IFileSystem> systemSaveDataFs
  266. public ResultCode OpenSaveDataFileSystemBySystemSaveDataId(ServiceCtx context)
  267. {
  268. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  269. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  270. using var fileSystem = new SharedRef<IFileSystem>();
  271. Result result = _baseFileSystemProxy.Get.OpenSaveDataFileSystemBySystemSaveDataId(ref fileSystem.Ref(), spaceId, in attribute);
  272. if (result.IsFailure()) return (ResultCode)result.Value;
  273. MakeObject(context, new FileSystemProxy.IFileSystem(ref fileSystem.Ref()));
  274. return ResultCode.Success;
  275. }
  276. [CommandHipc(53)]
  277. // OpenReadOnlySaveDataFileSystem(u8 spaceId, nn::fs::SaveDataAttribute attribute) -> object<nn::fssrv::sf::IFileSystem>
  278. public ResultCode OpenReadOnlySaveDataFileSystem(ServiceCtx context)
  279. {
  280. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  281. SaveDataAttribute attribute = context.RequestData.ReadStruct<SaveDataAttribute>();
  282. using var fileSystem = new SharedRef<IFileSystem>();
  283. Result result = _baseFileSystemProxy.Get.OpenReadOnlySaveDataFileSystem(ref fileSystem.Ref(), spaceId, in attribute);
  284. if (result.IsFailure()) return (ResultCode)result.Value;
  285. MakeObject(context, new FileSystemProxy.IFileSystem(ref fileSystem.Ref()));
  286. return ResultCode.Success;
  287. }
  288. [CommandHipc(57)]
  289. // ReadSaveDataFileSystemExtraDataBySaveDataSpaceId(u8 spaceId, u64 saveDataId) -> (buffer<nn::fs::SaveDataExtraData, 6> extraData)
  290. public ResultCode ReadSaveDataFileSystemExtraDataBySaveDataSpaceId(ServiceCtx context)
  291. {
  292. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  293. ulong saveDataId = context.RequestData.ReadUInt64();
  294. byte[] extraDataBuffer = new byte[context.Request.ReceiveBuff[0].Size];
  295. context.Memory.Read(context.Request.ReceiveBuff[0].Position, extraDataBuffer);
  296. Result result = _baseFileSystemProxy.Get.ReadSaveDataFileSystemExtraDataBySaveDataSpaceId(new OutBuffer(extraDataBuffer), spaceId, saveDataId);
  297. if (result.IsFailure()) return (ResultCode)result.Value;
  298. context.Memory.Write(context.Request.ReceiveBuff[0].Position, extraDataBuffer);
  299. return ResultCode.Success;
  300. }
  301. [CommandHipc(58)]
  302. // ReadSaveDataFileSystemExtraData(u64 saveDataId) -> (buffer<nn::fs::SaveDataExtraData, 6> extraData)
  303. public ResultCode ReadSaveDataFileSystemExtraData(ServiceCtx context)
  304. {
  305. ulong saveDataId = context.RequestData.ReadUInt64();
  306. byte[] extraDataBuffer = new byte[context.Request.ReceiveBuff[0].Size];
  307. context.Memory.Read(context.Request.ReceiveBuff[0].Position, extraDataBuffer);
  308. Result result = _baseFileSystemProxy.Get.ReadSaveDataFileSystemExtraData(new OutBuffer(extraDataBuffer), saveDataId);
  309. if (result.IsFailure()) return (ResultCode)result.Value;
  310. context.Memory.Write(context.Request.ReceiveBuff[0].Position, extraDataBuffer);
  311. return ResultCode.Success;
  312. }
  313. [CommandHipc(59)]
  314. // WriteSaveDataFileSystemExtraData(u8 spaceId, u64 saveDataId, buffer<nn::fs::SaveDataExtraData, 5> extraData) -> ()
  315. public ResultCode WriteSaveDataFileSystemExtraData(ServiceCtx context)
  316. {
  317. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  318. ulong saveDataId = context.RequestData.ReadUInt64();
  319. byte[] extraDataBuffer = new byte[context.Request.SendBuff[0].Size];
  320. context.Memory.Read(context.Request.SendBuff[0].Position, extraDataBuffer);
  321. return (ResultCode)_baseFileSystemProxy.Get.WriteSaveDataFileSystemExtraData(saveDataId, spaceId, new InBuffer(extraDataBuffer)).Value;
  322. }
  323. [CommandHipc(60)]
  324. // OpenSaveDataInfoReader() -> object<nn::fssrv::sf::ISaveDataInfoReader>
  325. public ResultCode OpenSaveDataInfoReader(ServiceCtx context)
  326. {
  327. using var infoReader = new SharedRef<LibHac.FsSrv.Sf.ISaveDataInfoReader>();
  328. Result result = _baseFileSystemProxy.Get.OpenSaveDataInfoReader(ref infoReader.Ref());
  329. if (result.IsFailure()) return (ResultCode)result.Value;
  330. MakeObject(context, new ISaveDataInfoReader(ref infoReader.Ref()));
  331. return ResultCode.Success;
  332. }
  333. [CommandHipc(61)]
  334. // OpenSaveDataInfoReaderBySaveDataSpaceId(u8 spaceId) -> object<nn::fssrv::sf::ISaveDataInfoReader>
  335. public ResultCode OpenSaveDataInfoReaderBySaveDataSpaceId(ServiceCtx context)
  336. {
  337. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadByte();
  338. using var infoReader = new SharedRef<LibHac.FsSrv.Sf.ISaveDataInfoReader>();
  339. Result result = _baseFileSystemProxy.Get.OpenSaveDataInfoReaderBySaveDataSpaceId(ref infoReader.Ref(), spaceId);
  340. if (result.IsFailure()) return (ResultCode)result.Value;
  341. MakeObject(context, new ISaveDataInfoReader(ref infoReader.Ref()));
  342. return ResultCode.Success;
  343. }
  344. [CommandHipc(62)]
  345. // OpenSaveDataInfoReaderOnlyCacheStorage() -> object<nn::fssrv::sf::ISaveDataInfoReader>
  346. public ResultCode OpenSaveDataInfoReaderOnlyCacheStorage(ServiceCtx context)
  347. {
  348. using var infoReader = new SharedRef<LibHac.FsSrv.Sf.ISaveDataInfoReader>();
  349. Result result = _baseFileSystemProxy.Get.OpenSaveDataInfoReaderOnlyCacheStorage(ref infoReader.Ref());
  350. if (result.IsFailure()) return (ResultCode)result.Value;
  351. MakeObject(context, new ISaveDataInfoReader(ref infoReader.Ref()));
  352. return ResultCode.Success;
  353. }
  354. [CommandHipc(64)]
  355. // OpenSaveDataInternalStorageFileSystem(u8 spaceId, u64 saveDataId) -> object<nn::fssrv::sf::ISaveDataInfoReader>
  356. public ResultCode OpenSaveDataInternalStorageFileSystem(ServiceCtx context)
  357. {
  358. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  359. ulong saveDataId = context.RequestData.ReadUInt64();
  360. using var fileSystem = new SharedRef<IFileSystem>();
  361. Result result = _baseFileSystemProxy.Get.OpenSaveDataInternalStorageFileSystem(ref fileSystem.Ref(), spaceId, saveDataId);
  362. if (result.IsFailure()) return (ResultCode)result.Value;
  363. MakeObject(context, new FileSystemProxy.IFileSystem(ref fileSystem.Ref()));
  364. return ResultCode.Success;
  365. }
  366. [CommandHipc(65)]
  367. // UpdateSaveDataMacForDebug(u8 spaceId, u64 saveDataId) -> ()
  368. public ResultCode UpdateSaveDataMacForDebug(ServiceCtx context)
  369. {
  370. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  371. ulong saveDataId = context.RequestData.ReadUInt64();
  372. return (ResultCode)_baseFileSystemProxy.Get.UpdateSaveDataMacForDebug(spaceId, saveDataId).Value;
  373. }
  374. [CommandHipc(66)]
  375. public ResultCode WriteSaveDataFileSystemExtraDataWithMask(ServiceCtx context)
  376. {
  377. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  378. ulong saveDataId = context.RequestData.ReadUInt64();
  379. byte[] extraDataBuffer = new byte[context.Request.SendBuff[0].Size];
  380. context.Memory.Read(context.Request.SendBuff[0].Position, extraDataBuffer);
  381. byte[] maskBuffer = new byte[context.Request.SendBuff[1].Size];
  382. context.Memory.Read(context.Request.SendBuff[1].Position, maskBuffer);
  383. return (ResultCode)_baseFileSystemProxy.Get.WriteSaveDataFileSystemExtraDataWithMask(saveDataId, spaceId, new InBuffer(extraDataBuffer), new InBuffer(maskBuffer)).Value;
  384. }
  385. [CommandHipc(67)]
  386. public ResultCode FindSaveDataWithFilter(ServiceCtx context)
  387. {
  388. SaveDataSpaceId spaceId = (SaveDataSpaceId)context.RequestData.ReadInt64();
  389. SaveDataFilter filter = context.RequestData.ReadStruct<SaveDataFilter>();
  390. ulong bufferPosition = context.Request.ReceiveBuff[0].Position;
  391. ulong bufferLen = context.Request.ReceiveBuff[0].Size;
  392. byte[] infoBuffer = new byte[bufferLen];
  393. Result result = _baseFileSystemProxy.Get.FindSaveDataWithFilter(out long count, new OutBuffer(infoBuffer), spaceId, in filter);
  394. if (result.IsFailure()) return (ResultCode)result.Value;
  395. context.Memory.Write(bufferPosition, infoBuffer);
  396. context.ResponseData.Write(count);
  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. }