IFileSystemProxy.cs 10.0 KB

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