IFileSystemProxy.cs 13 KB

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