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> _commands;
  16. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
  17. public IFileSystemProxy()
  18. {
  19. _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. }