IFileSystemProxy.cs 10 KB

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