FileSystemProxyHelper.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. var titleKey = ticket.GetTitleKey(keySet);
  103. if (titleKey != null)
  104. {
  105. keySet.ExternalKeySet.Add(new RightsId(ticket.RightsId), new AccessKey(titleKey));
  106. }
  107. }
  108. }
  109. }
  110. public static ref readonly FspPath GetFspPath(ServiceCtx context, int index = 0)
  111. {
  112. ulong position = context.Request.PtrBuff[index].Position;
  113. ulong size = context.Request.PtrBuff[index].Size;
  114. ReadOnlySpan<byte> buffer = context.Memory.GetSpan(position, (int)size);
  115. ReadOnlySpan<FspPath> fspBuffer = MemoryMarshal.Cast<byte, FspPath>(buffer);
  116. return ref fspBuffer[0];
  117. }
  118. public static ref readonly LibHac.FsSrv.Sf.Path GetSfPath(ServiceCtx context, int index = 0)
  119. {
  120. ulong position = context.Request.PtrBuff[index].Position;
  121. ulong size = context.Request.PtrBuff[index].Size;
  122. ReadOnlySpan<byte> buffer = context.Memory.GetSpan(position, (int)size);
  123. ReadOnlySpan<LibHac.FsSrv.Sf.Path> pathBuffer = MemoryMarshal.Cast<byte, LibHac.FsSrv.Sf.Path>(buffer);
  124. return ref pathBuffer[0];
  125. }
  126. }
  127. }