IFileSystemProxy.cs 12 KB

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