IFileSystemProxy.cs 12 KB

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