IFileSystemProxy.cs 13 KB

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