FileSystemProxyHelper.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using LibHac;
  2. using LibHac.Common;
  3. using LibHac.Common.Keys;
  4. using LibHac.Fs;
  5. using LibHac.FsSrv.Impl;
  6. using LibHac.FsSrv.Sf;
  7. using LibHac.FsSystem;
  8. using LibHac.Spl;
  9. using LibHac.Tools.Es;
  10. using LibHac.Tools.Fs;
  11. using LibHac.Tools.FsSystem;
  12. using LibHac.Tools.FsSystem.NcaUtils;
  13. using System;
  14. using System.IO;
  15. using System.Runtime.InteropServices;
  16. using Path = System.IO.Path;
  17. namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy
  18. {
  19. static class FileSystemProxyHelper
  20. {
  21. public static ResultCode OpenNsp(ServiceCtx context, string pfsPath, out IFileSystem openedFileSystem)
  22. {
  23. openedFileSystem = null;
  24. try
  25. {
  26. LocalStorage storage = new LocalStorage(pfsPath, FileAccess.Read, FileMode.Open);
  27. using SharedRef<LibHac.Fs.Fsa.IFileSystem> nsp = new(new PartitionFileSystem(storage));
  28. ImportTitleKeysFromNsp(nsp.Get, context.Device.System.KeySet);
  29. using SharedRef<LibHac.FsSrv.Sf.IFileSystem> adapter = FileSystemInterfaceAdapter.CreateShared(ref nsp.Ref(), true);
  30. openedFileSystem = new IFileSystem(ref adapter.Ref());
  31. }
  32. catch (HorizonResultException ex)
  33. {
  34. return (ResultCode)ex.ResultValue.Value;
  35. }
  36. return ResultCode.Success;
  37. }
  38. public static ResultCode OpenNcaFs(ServiceCtx context, string ncaPath, LibHac.Fs.IStorage ncaStorage, out IFileSystem openedFileSystem)
  39. {
  40. openedFileSystem = null;
  41. try
  42. {
  43. Nca nca = new Nca(context.Device.System.KeySet, ncaStorage);
  44. if (!nca.SectionExists(NcaSectionType.Data))
  45. {
  46. return ResultCode.PartitionNotFound;
  47. }
  48. LibHac.Fs.Fsa.IFileSystem fileSystem = nca.OpenFileSystem(NcaSectionType.Data, context.Device.System.FsIntegrityCheckLevel);
  49. using var sharedFs = new SharedRef<LibHac.Fs.Fsa.IFileSystem>(fileSystem);
  50. using SharedRef<LibHac.FsSrv.Sf.IFileSystem> adapter = FileSystemInterfaceAdapter.CreateShared(ref sharedFs.Ref(), true);
  51. openedFileSystem = new IFileSystem(ref adapter.Ref());
  52. }
  53. catch (HorizonResultException ex)
  54. {
  55. return (ResultCode)ex.ResultValue.Value;
  56. }
  57. return ResultCode.Success;
  58. }
  59. public static ResultCode OpenFileSystemFromInternalFile(ServiceCtx context, string fullPath, out IFileSystem openedFileSystem)
  60. {
  61. openedFileSystem = null;
  62. DirectoryInfo archivePath = new DirectoryInfo(fullPath).Parent;
  63. while (string.IsNullOrWhiteSpace(archivePath.Extension))
  64. {
  65. archivePath = archivePath.Parent;
  66. }
  67. if (archivePath.Extension == ".nsp" && File.Exists(archivePath.FullName))
  68. {
  69. FileStream pfsFile = new FileStream(
  70. archivePath.FullName.TrimEnd(Path.DirectorySeparatorChar),
  71. FileMode.Open,
  72. FileAccess.Read);
  73. try
  74. {
  75. PartitionFileSystem nsp = new PartitionFileSystem(pfsFile.AsStorage());
  76. ImportTitleKeysFromNsp(nsp, context.Device.System.KeySet);
  77. string filename = fullPath.Replace(archivePath.FullName, string.Empty).TrimStart('\\');
  78. using var ncaFile = new UniqueRef<LibHac.Fs.Fsa.IFile>();
  79. Result result = nsp.OpenFile(ref ncaFile.Ref(), filename.ToU8Span(), OpenMode.Read);
  80. if (result.IsFailure())
  81. {
  82. return (ResultCode)result.Value;
  83. }
  84. return OpenNcaFs(context, fullPath, ncaFile.Release().AsStorage(), out openedFileSystem);
  85. }
  86. catch (HorizonResultException ex)
  87. {
  88. return (ResultCode)ex.ResultValue.Value;
  89. }
  90. }
  91. return ResultCode.PathDoesNotExist;
  92. }
  93. public static void ImportTitleKeysFromNsp(LibHac.Fs.Fsa.IFileSystem nsp, KeySet keySet)
  94. {
  95. foreach (DirectoryEntryEx ticketEntry in nsp.EnumerateEntries("/", "*.tik"))
  96. {
  97. using var ticketFile = new UniqueRef<LibHac.Fs.Fsa.IFile>();
  98. Result result = nsp.OpenFile(ref ticketFile.Ref(), ticketEntry.FullPath.ToU8Span(), OpenMode.Read);
  99. if (result.IsSuccess())
  100. {
  101. Ticket ticket = new Ticket(ticketFile.Get.AsStream());
  102. keySet.ExternalKeySet.Add(new RightsId(ticket.RightsId), new AccessKey(ticket.GetTitleKey(keySet)));
  103. }
  104. }
  105. }
  106. public static Result ReadFsPath(out FsPath path, ServiceCtx context, int index = 0)
  107. {
  108. ulong position = context.Request.PtrBuff[index].Position;
  109. ulong size = context.Request.PtrBuff[index].Size;
  110. byte[] pathBytes = new byte[size];
  111. context.Memory.Read(position, pathBytes);
  112. return FsPath.FromSpan(out path, pathBytes);
  113. }
  114. public static ref readonly FspPath GetFspPath(ServiceCtx context, int index = 0)
  115. {
  116. ulong position = (ulong)context.Request.PtrBuff[index].Position;
  117. ulong size = (ulong)context.Request.PtrBuff[index].Size;
  118. ReadOnlySpan<byte> buffer = context.Memory.GetSpan(position, (int)size);
  119. ReadOnlySpan<FspPath> fspBuffer = MemoryMarshal.Cast<byte, FspPath>(buffer);
  120. return ref fspBuffer[0];
  121. }
  122. public static ref readonly LibHac.FsSrv.Sf.Path GetSfPath(ServiceCtx context, int index = 0)
  123. {
  124. ulong position = (ulong)context.Request.PtrBuff[index].Position;
  125. ulong size = (ulong)context.Request.PtrBuff[index].Size;
  126. ReadOnlySpan<byte> buffer = context.Memory.GetSpan(position, (int)size);
  127. ReadOnlySpan<LibHac.FsSrv.Sf.Path> pathBuffer = MemoryMarshal.Cast<byte, LibHac.FsSrv.Sf.Path>(buffer);
  128. return ref pathBuffer[0];
  129. }
  130. }
  131. }