IFileSystemProxy.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using LibHac;
  2. using LibHac.Fs;
  3. using LibHac.Fs.NcaUtils;
  4. using Ryujinx.HLE.FileSystem;
  5. using Ryujinx.HLE.HOS.Ipc;
  6. using Ryujinx.HLE.Utilities;
  7. using System.Collections.Generic;
  8. using System.IO;
  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 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 MakeError(ErrorModule.Fs, FsErr.InvalidInput);
  87. }
  88. string fullPath = context.Device.FileSystem.GetFullPartitionPath(bisPartitionPath);
  89. LocalFileSystem fileSystem = new LocalFileSystem(fullPath);
  90. MakeObject(context, new IFileSystem(fileSystem));
  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. LocalFileSystem fileSystem = new LocalFileSystem(sdCardPath);
  98. MakeObject(context, new IFileSystem(fileSystem));
  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.AsStorage()));
  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.PublicData;
  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.Fs.IStorage ncaStorage = new LocalStorage(ncaPath, FileAccess.Read, FileMode.Open);
  144. Nca nca = new Nca(context.Device.System.KeySet, ncaStorage);
  145. LibHac.Fs.IStorage romfsStorage = nca.OpenStorage(NcaSectionType.Data, context.Device.System.FsIntegrityCheckLevel);
  146. MakeObject(context, new IStorage(romfsStorage));
  147. return 0;
  148. }
  149. else
  150. {
  151. throw new FileNotFoundException($"No Nca found in Path `{ncaPath}`.");
  152. }
  153. }
  154. else
  155. {
  156. throw new DirectoryNotFoundException($"Path for title id {titleId:x16} on Storage {storageId} was not found in Path {installPath}.");
  157. }
  158. }
  159. throw new FileNotFoundException($"System archive with titleid {titleId:x16} was not found on Storage {storageId}. Found in {installedStorage}.");
  160. }
  161. // OpenPatchDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage>
  162. public long OpenPatchDataStorageByCurrentProcess(ServiceCtx context)
  163. {
  164. MakeObject(context, new IStorage(context.Device.FileSystem.RomFs.AsStorage()));
  165. return 0;
  166. }
  167. // GetGlobalAccessLogMode() -> u32 logMode
  168. public long GetGlobalAccessLogMode(ServiceCtx context)
  169. {
  170. context.ResponseData.Write(0);
  171. return 0;
  172. }
  173. public void LoadSaveDataFileSystem(ServiceCtx context)
  174. {
  175. SaveSpaceId saveSpaceId = (SaveSpaceId)context.RequestData.ReadInt64();
  176. long titleId = context.RequestData.ReadInt64();
  177. UInt128 userId = new UInt128(
  178. context.RequestData.ReadInt64(),
  179. context.RequestData.ReadInt64());
  180. long saveId = context.RequestData.ReadInt64();
  181. SaveDataType saveDataType = (SaveDataType)context.RequestData.ReadByte();
  182. SaveInfo saveInfo = new SaveInfo(titleId, saveId, saveDataType, userId, saveSpaceId);
  183. string savePath = context.Device.FileSystem.GetGameSavePath(saveInfo, context);
  184. LocalFileSystem fileSystem = new LocalFileSystem(savePath);
  185. DirectorySaveDataFileSystem saveFileSystem = new DirectorySaveDataFileSystem(fileSystem);
  186. MakeObject(context, new IFileSystem(saveFileSystem));
  187. }
  188. private long OpenNsp(ServiceCtx context, string pfsPath)
  189. {
  190. LocalStorage storage = new LocalStorage(pfsPath, FileAccess.Read, FileMode.Open);
  191. PartitionFileSystem nsp = new PartitionFileSystem(storage);
  192. ImportTitleKeysFromNsp(nsp, context.Device.System.KeySet);
  193. IFileSystem nspFileSystem = new IFileSystem(nsp);
  194. MakeObject(context, nspFileSystem);
  195. return 0;
  196. }
  197. private long OpenNcaFs(ServiceCtx context, string ncaPath, LibHac.Fs.IStorage ncaStorage)
  198. {
  199. Nca nca = new Nca(context.Device.System.KeySet, ncaStorage);
  200. if (!nca.SectionExists(NcaSectionType.Data))
  201. {
  202. return MakeError(ErrorModule.Fs, FsErr.PartitionNotFound);
  203. }
  204. LibHac.Fs.IFileSystem fileSystem = nca.OpenFileSystem(NcaSectionType.Data, context.Device.System.FsIntegrityCheckLevel);
  205. MakeObject(context, new IFileSystem(fileSystem));
  206. return 0;
  207. }
  208. private long OpenFileSystemFromInternalFile(ServiceCtx context, string fullPath)
  209. {
  210. DirectoryInfo archivePath = new DirectoryInfo(fullPath).Parent;
  211. while (string.IsNullOrWhiteSpace(archivePath.Extension))
  212. {
  213. archivePath = archivePath.Parent;
  214. }
  215. if (archivePath.Extension == ".nsp" && File.Exists(archivePath.FullName))
  216. {
  217. FileStream pfsFile = new FileStream(
  218. archivePath.FullName.TrimEnd(Path.DirectorySeparatorChar),
  219. FileMode.Open,
  220. FileAccess.Read);
  221. PartitionFileSystem nsp = new PartitionFileSystem(pfsFile.AsStorage());
  222. ImportTitleKeysFromNsp(nsp, context.Device.System.KeySet);
  223. string filename = fullPath.Replace(archivePath.FullName, string.Empty).TrimStart('\\');
  224. if (nsp.FileExists(filename))
  225. {
  226. return OpenNcaFs(context, fullPath, nsp.OpenFile(filename, OpenMode.Read).AsStorage());
  227. }
  228. }
  229. return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
  230. }
  231. private void ImportTitleKeysFromNsp(LibHac.Fs.IFileSystem nsp, Keyset keySet)
  232. {
  233. foreach (LibHac.Fs.DirectoryEntry ticketEntry in nsp.EnumerateEntries("*.tik"))
  234. {
  235. Ticket ticket = new Ticket(nsp.OpenFile(ticketEntry.FullPath, OpenMode.Read).AsStream());
  236. if (!keySet.TitleKeys.ContainsKey(ticket.RightsId))
  237. {
  238. keySet.TitleKeys.Add(ticket.RightsId, ticket.GetTitleKey(keySet));
  239. }
  240. }
  241. }
  242. }
  243. }