IFileSystemProxy.cs 13 KB

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