FileSystemProxyHelper.cs 6.0 KB

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