FileSystemProxyHelper.cs 5.7 KB

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