IFileSystemProxy.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using LibHac;
  2. using Ryujinx.HLE.FileSystem;
  3. using Ryujinx.HLE.HOS.Ipc;
  4. using Ryujinx.HLE.Utilities;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using static Ryujinx.HLE.FileSystem.VirtualFileSystem;
  9. using static Ryujinx.HLE.HOS.ErrorCode;
  10. using static Ryujinx.HLE.Utilities.StringUtils;
  11. namespace Ryujinx.HLE.HOS.Services.FspSrv
  12. {
  13. class IFileSystemProxy : IpcService
  14. {
  15. private Dictionary<int, ServiceProcessRequest> m_Commands;
  16. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  17. public IFileSystemProxy()
  18. {
  19. m_Commands = new Dictionary<int, ServiceProcessRequest>()
  20. {
  21. { 1, Initialize },
  22. { 8, OpenFileSystemWithId },
  23. { 11, OpenBisFileSystem },
  24. { 18, OpenSdCardFileSystem },
  25. { 51, OpenSaveDataFileSystem },
  26. { 52, OpenSaveDataFileSystemBySystemSaveDataId },
  27. { 200, OpenDataStorageByCurrentProcess },
  28. { 202, OpenDataStorageByDataId },
  29. { 203, OpenPatchDataStorageByCurrentProcess },
  30. { 1005, GetGlobalAccessLogMode }
  31. };
  32. }
  33. // Initialize(u64, pid)
  34. public long Initialize(ServiceCtx Context)
  35. {
  36. return 0;
  37. }
  38. // OpenFileSystemWithId(nn::fssrv::sf::FileSystemType filesystem_type, nn::ApplicationId tid, buffer<bytes<0x301>, 0x19, 0x301> path)
  39. // -> object<nn::fssrv::sf::IFileSystem> contentFs
  40. public long OpenFileSystemWithId(ServiceCtx Context)
  41. {
  42. FileSystemType FileSystemType = (FileSystemType)Context.RequestData.ReadInt32();
  43. long TitleId = Context.RequestData.ReadInt64();
  44. string SwitchPath = ReadUtf8String(Context);
  45. string FullPath = Context.Device.FileSystem.SwitchPathToSystemPath(SwitchPath);
  46. if (!File.Exists(FullPath))
  47. {
  48. if (FullPath.Contains("."))
  49. {
  50. return OpenFileSystemFromInternalFile(Context, FullPath);
  51. }
  52. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  53. }
  54. FileStream FileStream = new FileStream(FullPath, FileMode.Open, FileAccess.Read);
  55. string Extension = Path.GetExtension(FullPath);
  56. if (Extension == ".nca")
  57. {
  58. return OpenNcaFs(Context, FullPath, FileStream);
  59. }
  60. else if (Extension == ".nsp")
  61. {
  62. return OpenNsp(Context, FullPath);
  63. }
  64. return MakeError(ErrorModule.Fs, FsErr.InvalidInput);
  65. }
  66. // OpenBisFileSystem(nn::fssrv::sf::Partition partitionID, buffer<bytes<0x301>, 0x19, 0x301>) -> object<nn::fssrv::sf::IFileSystem> Bis
  67. public long OpenBisFileSystem(ServiceCtx Context)
  68. {
  69. int BisPartitionId = Context.RequestData.ReadInt32();
  70. string PartitionString = ReadUtf8String(Context);
  71. string BisPartitonPath = string.Empty;
  72. switch (BisPartitionId)
  73. {
  74. case 29:
  75. BisPartitonPath = SafeNandPath;
  76. break;
  77. case 30:
  78. case 31:
  79. BisPartitonPath = SystemNandPath;
  80. break;
  81. case 32:
  82. BisPartitonPath = UserNandPath;
  83. break;
  84. default:
  85. return MakeError(ErrorModule.Fs, FsErr.InvalidInput);
  86. }
  87. string FullPath = Context.Device.FileSystem.GetFullPartitionPath(BisPartitonPath);
  88. FileSystemProvider FileSystemProvider = new FileSystemProvider(FullPath, Context.Device.FileSystem.GetBasePath());
  89. MakeObject(Context, new IFileSystem(FullPath, FileSystemProvider));
  90. return 0;
  91. }
  92. // OpenSdCardFileSystem() -> object<nn::fssrv::sf::IFileSystem>
  93. public long OpenSdCardFileSystem(ServiceCtx Context)
  94. {
  95. string SdCardPath = Context.Device.FileSystem.GetSdCardPath();
  96. FileSystemProvider FileSystemProvider = new FileSystemProvider(SdCardPath, Context.Device.FileSystem.GetBasePath());
  97. MakeObject(Context, new IFileSystem(SdCardPath, FileSystemProvider));
  98. return 0;
  99. }
  100. // OpenSaveDataFileSystem(u8 save_data_space_id, nn::fssrv::sf::SaveStruct saveStruct) -> object<nn::fssrv::sf::IFileSystem> saveDataFs
  101. public long OpenSaveDataFileSystem(ServiceCtx Context)
  102. {
  103. LoadSaveDataFileSystem(Context);
  104. return 0;
  105. }
  106. // OpenSaveDataFileSystemBySystemSaveDataId(u8 save_data_space_id, nn::fssrv::sf::SaveStruct saveStruct) -> object<nn::fssrv::sf::IFileSystem> systemSaveDataFs
  107. public long OpenSaveDataFileSystemBySystemSaveDataId(ServiceCtx Context)
  108. {
  109. LoadSaveDataFileSystem(Context);
  110. return 0;
  111. }
  112. // OpenDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage> dataStorage
  113. public long OpenDataStorageByCurrentProcess(ServiceCtx Context)
  114. {
  115. MakeObject(Context, new IStorage(Context.Device.FileSystem.RomFs));
  116. return 0;
  117. }
  118. // OpenDataStorageByDataId(u8 storageId, nn::ApplicationId tid) -> object<nn::fssrv::sf::IStorage> dataStorage
  119. public long OpenDataStorageByDataId(ServiceCtx Context)
  120. {
  121. StorageId StorageId = (StorageId)Context.RequestData.ReadByte();
  122. byte[] Padding = Context.RequestData.ReadBytes(7);
  123. long TitleId = Context.RequestData.ReadInt64();
  124. StorageId InstalledStorage =
  125. Context.Device.System.ContentManager.GetInstalledStorage(TitleId, ContentType.Data, StorageId);
  126. if (InstalledStorage == StorageId.None)
  127. {
  128. InstalledStorage =
  129. Context.Device.System.ContentManager.GetInstalledStorage(TitleId, ContentType.AocData, StorageId);
  130. }
  131. if (InstalledStorage != StorageId.None)
  132. {
  133. string ContentPath = Context.Device.System.ContentManager.GetInstalledContentPath(TitleId, StorageId, ContentType.AocData);
  134. if (string.IsNullOrWhiteSpace(ContentPath))
  135. {
  136. ContentPath = Context.Device.System.ContentManager.GetInstalledContentPath(TitleId, StorageId, ContentType.AocData);
  137. }
  138. string InstallPath = Context.Device.FileSystem.SwitchPathToSystemPath(ContentPath);
  139. if (!string.IsNullOrWhiteSpace(InstallPath))
  140. {
  141. string NcaPath = InstallPath;
  142. if (File.Exists(NcaPath))
  143. {
  144. FileStream NcaStream = new FileStream(NcaPath, FileMode.Open, FileAccess.Read);
  145. Nca Nca = new Nca(Context.Device.System.KeySet, NcaStream, false);
  146. NcaSection RomfsSection = Nca.Sections.FirstOrDefault(x => x?.Type == SectionType.Romfs);
  147. Stream RomfsStream = Nca.OpenSection(RomfsSection.SectionNum, false, Context.Device.System.FsIntegrityCheckLevel);
  148. MakeObject(Context, new IStorage(RomfsStream));
  149. return 0;
  150. }
  151. else
  152. {
  153. throw new FileNotFoundException($"No Nca found in Path `{NcaPath}`.");
  154. }
  155. }
  156. else
  157. {
  158. throw new DirectoryNotFoundException($"Path for title id {TitleId:x16} on Storage {StorageId} was not found in Path {InstallPath}.");
  159. }
  160. }
  161. throw new FileNotFoundException($"System archive with titleid {TitleId:x16} was not found on Storage {StorageId}. Found in {InstalledStorage}.");
  162. }
  163. // OpenPatchDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage>
  164. public long OpenPatchDataStorageByCurrentProcess(ServiceCtx Context)
  165. {
  166. MakeObject(Context, new IStorage(Context.Device.FileSystem.RomFs));
  167. return 0;
  168. }
  169. // GetGlobalAccessLogMode() -> u32 logMode
  170. public long GetGlobalAccessLogMode(ServiceCtx Context)
  171. {
  172. Context.ResponseData.Write(0);
  173. return 0;
  174. }
  175. public void LoadSaveDataFileSystem(ServiceCtx Context)
  176. {
  177. SaveSpaceId SaveSpaceId = (SaveSpaceId)Context.RequestData.ReadInt64();
  178. long TitleId = Context.RequestData.ReadInt64();
  179. UInt128 UserId = new UInt128(
  180. Context.RequestData.ReadInt64(),
  181. Context.RequestData.ReadInt64());
  182. long SaveId = Context.RequestData.ReadInt64();
  183. SaveDataType SaveDataType = (SaveDataType)Context.RequestData.ReadByte();
  184. SaveInfo SaveInfo = new SaveInfo(TitleId, SaveId, SaveDataType, UserId, SaveSpaceId);
  185. string SavePath = Context.Device.FileSystem.GetGameSavePath(SaveInfo, Context);
  186. FileSystemProvider FileSystemProvider = new FileSystemProvider(SavePath, Context.Device.FileSystem.GetBasePath());
  187. MakeObject(Context, new IFileSystem(SavePath, FileSystemProvider));
  188. }
  189. private long OpenNsp(ServiceCtx Context, string PfsPath)
  190. {
  191. FileStream PfsFile = new FileStream(PfsPath, FileMode.Open, FileAccess.Read);
  192. Pfs Nsp = new Pfs(PfsFile);
  193. PfsFileEntry TicketFile = Nsp.Files.FirstOrDefault(x => x.Name.EndsWith(".tik"));
  194. if (TicketFile != null)
  195. {
  196. Ticket Ticket = new Ticket(Nsp.OpenFile(TicketFile));
  197. Context.Device.System.KeySet.TitleKeys[Ticket.RightsId] =
  198. Ticket.GetTitleKey(Context.Device.System.KeySet);
  199. }
  200. IFileSystem NspFileSystem = new IFileSystem(PfsPath, new PFsProvider(Nsp));
  201. MakeObject(Context, NspFileSystem);
  202. return 0;
  203. }
  204. private long OpenNcaFs(ServiceCtx Context,string NcaPath, Stream NcaStream)
  205. {
  206. Nca Nca = new Nca(Context.Device.System.KeySet, NcaStream, false);
  207. NcaSection RomfsSection = Nca.Sections.FirstOrDefault(x => x?.Type == SectionType.Romfs);
  208. NcaSection PfsSection = Nca.Sections.FirstOrDefault(x => x?.Type == SectionType.Pfs0);
  209. if (RomfsSection != null)
  210. {
  211. Stream RomfsStream = Nca.OpenSection(RomfsSection.SectionNum, false, Context.Device.System.FsIntegrityCheckLevel);
  212. IFileSystem NcaFileSystem = new IFileSystem(NcaPath, new RomFsProvider(RomfsStream));
  213. MakeObject(Context, NcaFileSystem);
  214. }
  215. else if(PfsSection !=null)
  216. {
  217. Stream PfsStream = Nca.OpenSection(PfsSection.SectionNum, false, Context.Device.System.FsIntegrityCheckLevel);
  218. Pfs Pfs = new Pfs(PfsStream);
  219. IFileSystem NcaFileSystem = new IFileSystem(NcaPath, new PFsProvider(Pfs));
  220. MakeObject(Context, NcaFileSystem);
  221. }
  222. else
  223. {
  224. return MakeError(ErrorModule.Fs, FsErr.PartitionNotFound);
  225. }
  226. return 0;
  227. }
  228. private long OpenFileSystemFromInternalFile(ServiceCtx Context, string FullPath)
  229. {
  230. DirectoryInfo ArchivePath = new DirectoryInfo(FullPath).Parent;
  231. while (string.IsNullOrWhiteSpace(ArchivePath.Extension))
  232. {
  233. ArchivePath = ArchivePath.Parent;
  234. }
  235. if (ArchivePath.Extension == ".nsp" && File.Exists(ArchivePath.FullName))
  236. {
  237. FileStream PfsFile = new FileStream(
  238. ArchivePath.FullName.TrimEnd(Path.DirectorySeparatorChar),
  239. FileMode.Open,
  240. FileAccess.Read);
  241. Pfs Nsp = new Pfs(PfsFile);
  242. PfsFileEntry TicketFile = Nsp.Files.FirstOrDefault(x => x.Name.EndsWith(".tik"));
  243. if (TicketFile != null)
  244. {
  245. Ticket Ticket = new Ticket(Nsp.OpenFile(TicketFile));
  246. Context.Device.System.KeySet.TitleKeys[Ticket.RightsId] =
  247. Ticket.GetTitleKey(Context.Device.System.KeySet);
  248. }
  249. string Filename = FullPath.Replace(ArchivePath.FullName, string.Empty).TrimStart('\\');
  250. if (Nsp.FileExists(Filename))
  251. {
  252. return OpenNcaFs(Context, FullPath, Nsp.OpenFile(Filename));
  253. }
  254. }
  255. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  256. }
  257. }
  258. }