IFileSystemProxy.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using LibHac;
  2. using LibHac.Fs;
  3. using LibHac.FsService;
  4. using LibHac.FsSystem;
  5. using LibHac.FsSystem.NcaUtils;
  6. using Ryujinx.Common.Logging;
  7. using Ryujinx.HLE.FileSystem;
  8. using Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy;
  9. using System.IO;
  10. using static Ryujinx.HLE.FileSystem.VirtualFileSystem;
  11. using static Ryujinx.HLE.Utilities.StringUtils;
  12. namespace Ryujinx.HLE.HOS.Services.Fs
  13. {
  14. [Service("fsp-srv")]
  15. class IFileSystemProxy : IpcService
  16. {
  17. private LibHac.FsService.IFileSystemProxy _baseFileSystemProxy;
  18. public IFileSystemProxy(ServiceCtx context)
  19. {
  20. _baseFileSystemProxy = context.Device.System.FsServer.CreateFileSystemProxyService();
  21. }
  22. [Command(1)]
  23. // Initialize(u64, pid)
  24. public ResultCode Initialize(ServiceCtx context)
  25. {
  26. return ResultCode.Success;
  27. }
  28. [Command(8)]
  29. // OpenFileSystemWithId(nn::fssrv::sf::FileSystemType filesystem_type, nn::ApplicationId tid, buffer<bytes<0x301>, 0x19, 0x301> path)
  30. // -> object<nn::fssrv::sf::IFileSystem> contentFs
  31. public ResultCode OpenFileSystemWithId(ServiceCtx context)
  32. {
  33. FileSystemType fileSystemType = (FileSystemType)context.RequestData.ReadInt32();
  34. long titleId = context.RequestData.ReadInt64();
  35. string switchPath = ReadUtf8String(context);
  36. string fullPath = context.Device.FileSystem.SwitchPathToSystemPath(switchPath);
  37. if (!File.Exists(fullPath))
  38. {
  39. if (fullPath.Contains("."))
  40. {
  41. ResultCode result = FileSystemProxyHelper.OpenFileSystemFromInternalFile(context, fullPath, out FileSystemProxy.IFileSystem fileSystem);
  42. if (result == ResultCode.Success)
  43. {
  44. MakeObject(context, fileSystem);
  45. }
  46. return result;
  47. }
  48. return ResultCode.PathDoesNotExist;
  49. }
  50. FileStream fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
  51. string extension = Path.GetExtension(fullPath);
  52. if (extension == ".nca")
  53. {
  54. ResultCode result = FileSystemProxyHelper.OpenNcaFs(context, fullPath, fileStream.AsStorage(), out FileSystemProxy.IFileSystem fileSystem);
  55. if (result == ResultCode.Success)
  56. {
  57. MakeObject(context, fileSystem);
  58. }
  59. return result;
  60. }
  61. else if (extension == ".nsp")
  62. {
  63. ResultCode result = FileSystemProxyHelper.OpenNsp(context, fullPath, out FileSystemProxy.IFileSystem fileSystem);
  64. if (result == ResultCode.Success)
  65. {
  66. MakeObject(context, fileSystem);
  67. }
  68. return result;
  69. }
  70. return ResultCode.InvalidInput;
  71. }
  72. [Command(11)]
  73. // OpenBisFileSystem(nn::fssrv::sf::Partition partitionID, buffer<bytes<0x301>, 0x19, 0x301>) -> object<nn::fssrv::sf::IFileSystem> Bis
  74. public ResultCode OpenBisFileSystem(ServiceCtx context)
  75. {
  76. int bisPartitionId = context.RequestData.ReadInt32();
  77. string partitionString = ReadUtf8String(context);
  78. string bisPartitionPath = string.Empty;
  79. switch (bisPartitionId)
  80. {
  81. case 29:
  82. bisPartitionPath = SafeNandPath;
  83. break;
  84. case 30:
  85. case 31:
  86. bisPartitionPath = SystemNandPath;
  87. break;
  88. case 32:
  89. bisPartitionPath = UserNandPath;
  90. break;
  91. default:
  92. return ResultCode.InvalidInput;
  93. }
  94. string fullPath = context.Device.FileSystem.GetFullPartitionPath(bisPartitionPath);
  95. LocalFileSystem fileSystem = new LocalFileSystem(fullPath);
  96. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  97. return ResultCode.Success;
  98. }
  99. [Command(18)]
  100. // OpenSdCardFileSystem() -> object<nn::fssrv::sf::IFileSystem>
  101. public ResultCode OpenSdCardFileSystem(ServiceCtx context)
  102. {
  103. string sdCardPath = context.Device.FileSystem.GetSdCardPath();
  104. LocalFileSystem fileSystem = new LocalFileSystem(sdCardPath);
  105. MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
  106. return ResultCode.Success;
  107. }
  108. [Command(30)]
  109. // OpenGameCardStorage(u32, u32) -> object<nn::fssrv::sf::IStorage>
  110. public ResultCode OpenGameCardStorage(ServiceCtx context)
  111. {
  112. GameCardHandle handle = new GameCardHandle(context.RequestData.ReadInt32());
  113. GameCardPartitionRaw partitionId = (GameCardPartitionRaw)context.RequestData.ReadInt32();
  114. Result result = _baseFileSystemProxy.OpenGameCardStorage(out LibHac.Fs.IStorage storage, handle, partitionId);
  115. if (result.IsSuccess())
  116. {
  117. MakeObject(context, new FileSystemProxy.IStorage(storage));
  118. }
  119. return (ResultCode)result.Value;
  120. }
  121. [Command(51)]
  122. // OpenSaveDataFileSystem(u8 save_data_space_id, nn::fssrv::sf::SaveStruct saveStruct) -> object<nn::fssrv::sf::IFileSystem> saveDataFs
  123. public ResultCode OpenSaveDataFileSystem(ServiceCtx context)
  124. {
  125. ResultCode result = FileSystemProxyHelper.LoadSaveDataFileSystem(context, false, out FileSystemProxy.IFileSystem fileSystem);
  126. if (result == ResultCode.Success)
  127. {
  128. MakeObject(context, fileSystem);
  129. }
  130. return result;
  131. }
  132. [Command(52)]
  133. // OpenSaveDataFileSystemBySystemSaveDataId(u8 save_data_space_id, nn::fssrv::sf::SaveStruct saveStruct) -> object<nn::fssrv::sf::IFileSystem> systemSaveDataFs
  134. public ResultCode OpenSaveDataFileSystemBySystemSaveDataId(ServiceCtx context)
  135. {
  136. ResultCode result = FileSystemProxyHelper.LoadSaveDataFileSystem(context, false, out FileSystemProxy.IFileSystem fileSystem);
  137. if (result == ResultCode.Success)
  138. {
  139. MakeObject(context, fileSystem);
  140. }
  141. return result;
  142. }
  143. [Command(53)]
  144. // OpenReadOnlySaveDataFileSystem(u8 save_data_space_id, nn::fssrv::sf::SaveStruct save_struct) -> object<nn::fssrv::sf::IFileSystem>
  145. public ResultCode OpenReadOnlySaveDataFileSystem(ServiceCtx context)
  146. {
  147. ResultCode result = FileSystemProxyHelper.LoadSaveDataFileSystem(context, true, out FileSystemProxy.IFileSystem fileSystem);
  148. if (result == ResultCode.Success)
  149. {
  150. MakeObject(context, fileSystem);
  151. }
  152. return result;
  153. }
  154. [Command(200)]
  155. // OpenDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage> dataStorage
  156. public ResultCode OpenDataStorageByCurrentProcess(ServiceCtx context)
  157. {
  158. MakeObject(context, new FileSystemProxy.IStorage(context.Device.FileSystem.RomFs.AsStorage()));
  159. return 0;
  160. }
  161. [Command(202)]
  162. // OpenDataStorageByDataId(u8 storageId, nn::ApplicationId tid) -> object<nn::fssrv::sf::IStorage> dataStorage
  163. public ResultCode OpenDataStorageByDataId(ServiceCtx context)
  164. {
  165. StorageId storageId = (StorageId)context.RequestData.ReadByte();
  166. byte[] padding = context.RequestData.ReadBytes(7);
  167. long titleId = context.RequestData.ReadInt64();
  168. NcaContentType contentType = NcaContentType.Data;
  169. StorageId installedStorage =
  170. context.Device.System.ContentManager.GetInstalledStorage(titleId, contentType, storageId);
  171. if (installedStorage == StorageId.None)
  172. {
  173. contentType = NcaContentType.PublicData;
  174. installedStorage =
  175. context.Device.System.ContentManager.GetInstalledStorage(titleId, contentType, storageId);
  176. }
  177. if (installedStorage != StorageId.None)
  178. {
  179. string contentPath = context.Device.System.ContentManager.GetInstalledContentPath(titleId, storageId, contentType);
  180. string installPath = context.Device.FileSystem.SwitchPathToSystemPath(contentPath);
  181. if (!string.IsNullOrWhiteSpace(installPath))
  182. {
  183. string ncaPath = installPath;
  184. if (File.Exists(ncaPath))
  185. {
  186. try
  187. {
  188. LibHac.Fs.IStorage ncaStorage = new LocalStorage(ncaPath, FileAccess.Read, FileMode.Open);
  189. Nca nca = new Nca(context.Device.System.KeySet, ncaStorage);
  190. LibHac.Fs.IStorage romfsStorage = nca.OpenStorage(NcaSectionType.Data, context.Device.System.FsIntegrityCheckLevel);
  191. MakeObject(context, new FileSystemProxy.IStorage(romfsStorage));
  192. }
  193. catch (HorizonResultException ex)
  194. {
  195. return (ResultCode)ex.ResultValue.Value;
  196. }
  197. return ResultCode.Success;
  198. }
  199. else
  200. {
  201. throw new FileNotFoundException($"No Nca found in Path `{ncaPath}`.");
  202. }
  203. }
  204. else
  205. {
  206. throw new DirectoryNotFoundException($"Path for title id {titleId:x16} on Storage {storageId} was not found in Path {installPath}.");
  207. }
  208. }
  209. throw new FileNotFoundException($"System archive with titleid {titleId:x16} was not found on Storage {storageId}. Found in {installedStorage}.");
  210. }
  211. [Command(203)]
  212. // OpenPatchDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage>
  213. public ResultCode OpenPatchDataStorageByCurrentProcess(ServiceCtx context)
  214. {
  215. MakeObject(context, new FileSystemProxy.IStorage(context.Device.FileSystem.RomFs.AsStorage()));
  216. return ResultCode.Success;
  217. }
  218. [Command(400)]
  219. // OpenDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage> dataStorage
  220. public ResultCode OpenDeviceOperator(ServiceCtx context)
  221. {
  222. Result result = _baseFileSystemProxy.OpenDeviceOperator(out LibHac.FsService.IDeviceOperator deviceOperator);
  223. if (result.IsSuccess())
  224. {
  225. MakeObject(context, new IDeviceOperator(deviceOperator));
  226. }
  227. return (ResultCode)result.Value;
  228. }
  229. [Command(1005)]
  230. // GetGlobalAccessLogMode() -> u32 logMode
  231. public ResultCode GetGlobalAccessLogMode(ServiceCtx context)
  232. {
  233. int mode = context.Device.System.GlobalAccessLogMode;
  234. context.ResponseData.Write(mode);
  235. return ResultCode.Success;
  236. }
  237. [Command(1006)]
  238. // OutputAccessLogToSdCard(buffer<bytes, 5> log_text)
  239. public ResultCode OutputAccessLogToSdCard(ServiceCtx context)
  240. {
  241. string message = ReadUtf8StringSend(context);
  242. // FS ends each line with a newline. Remove it because Ryujinx logging adds its own newline
  243. Logger.PrintAccessLog(LogClass.ServiceFs, message.TrimEnd('\n'));
  244. return ResultCode.Success;
  245. }
  246. }
  247. }