2019-09-08 21:33:40 +00:00
|
|
|
|
using LibHac;
|
2020-03-25 08:14:35 +00:00
|
|
|
|
using LibHac.Common;
|
2021-08-12 21:56:24 +00:00
|
|
|
|
using LibHac.Common.Keys;
|
2019-09-08 21:33:40 +00:00
|
|
|
|
using LibHac.Fs;
|
2021-08-12 21:56:24 +00:00
|
|
|
|
using LibHac.FsSrv.Impl;
|
|
|
|
|
using LibHac.FsSrv.Sf;
|
2019-10-17 06:17:44 +00:00
|
|
|
|
using LibHac.FsSystem;
|
|
|
|
|
using LibHac.FsSystem.NcaUtils;
|
|
|
|
|
using LibHac.Spl;
|
2021-08-12 21:56:24 +00:00
|
|
|
|
using System;
|
2019-09-08 21:33:40 +00:00
|
|
|
|
using System.IO;
|
2021-08-12 21:56:24 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using Path = System.IO.Path;
|
2019-09-08 21:33:40 +00:00
|
|
|
|
|
2019-09-19 00:45:11 +00:00
|
|
|
|
namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy
|
2019-09-08 21:33:40 +00:00
|
|
|
|
{
|
2019-09-19 00:45:11 +00:00
|
|
|
|
static class FileSystemProxyHelper
|
2019-09-08 21:33:40 +00:00
|
|
|
|
{
|
|
|
|
|
public static ResultCode OpenNsp(ServiceCtx context, string pfsPath, out IFileSystem openedFileSystem)
|
|
|
|
|
{
|
|
|
|
|
openedFileSystem = null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-08-12 21:56:24 +00:00
|
|
|
|
LocalStorage storage = new LocalStorage(pfsPath, FileAccess.Read, FileMode.Open);
|
2021-12-23 16:55:50 +00:00
|
|
|
|
using SharedRef<LibHac.Fs.Fsa.IFileSystem> nsp = new(new PartitionFileSystem(storage));
|
2019-09-08 21:33:40 +00:00
|
|
|
|
|
2021-12-23 16:55:50 +00:00
|
|
|
|
ImportTitleKeysFromNsp(nsp.Get, context.Device.System.KeySet);
|
2019-09-08 21:33:40 +00:00
|
|
|
|
|
2021-12-23 16:55:50 +00:00
|
|
|
|
using SharedRef<LibHac.FsSrv.Sf.IFileSystem> adapter = FileSystemInterfaceAdapter.CreateShared(ref nsp.Ref(), true);
|
|
|
|
|
|
|
|
|
|
openedFileSystem = new IFileSystem(ref adapter.Ref());
|
2019-09-08 21:33:40 +00:00
|
|
|
|
}
|
|
|
|
|
catch (HorizonResultException ex)
|
|
|
|
|
{
|
|
|
|
|
return (ResultCode)ex.ResultValue.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ResultCode OpenNcaFs(ServiceCtx context, string ncaPath, LibHac.Fs.IStorage ncaStorage, out IFileSystem openedFileSystem)
|
|
|
|
|
{
|
|
|
|
|
openedFileSystem = null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Nca nca = new Nca(context.Device.System.KeySet, ncaStorage);
|
|
|
|
|
|
|
|
|
|
if (!nca.SectionExists(NcaSectionType.Data))
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.PartitionNotFound;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-01 20:08:59 +00:00
|
|
|
|
LibHac.Fs.Fsa.IFileSystem fileSystem = nca.OpenFileSystem(NcaSectionType.Data, context.Device.System.FsIntegrityCheckLevel);
|
2021-12-23 16:55:50 +00:00
|
|
|
|
using var sharedFs = new SharedRef<LibHac.Fs.Fsa.IFileSystem>(fileSystem);
|
|
|
|
|
|
|
|
|
|
using SharedRef<LibHac.FsSrv.Sf.IFileSystem> adapter = FileSystemInterfaceAdapter.CreateShared(ref sharedFs.Ref(), true);
|
2019-09-08 21:33:40 +00:00
|
|
|
|
|
2021-12-23 16:55:50 +00:00
|
|
|
|
openedFileSystem = new IFileSystem(ref adapter.Ref());
|
2019-09-08 21:33:40 +00:00
|
|
|
|
}
|
|
|
|
|
catch (HorizonResultException ex)
|
|
|
|
|
{
|
|
|
|
|
return (ResultCode)ex.ResultValue.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ResultCode OpenFileSystemFromInternalFile(ServiceCtx context, string fullPath, out IFileSystem openedFileSystem)
|
|
|
|
|
{
|
|
|
|
|
openedFileSystem = null;
|
|
|
|
|
|
|
|
|
|
DirectoryInfo archivePath = new DirectoryInfo(fullPath).Parent;
|
|
|
|
|
|
|
|
|
|
while (string.IsNullOrWhiteSpace(archivePath.Extension))
|
|
|
|
|
{
|
|
|
|
|
archivePath = archivePath.Parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (archivePath.Extension == ".nsp" && File.Exists(archivePath.FullName))
|
|
|
|
|
{
|
|
|
|
|
FileStream pfsFile = new FileStream(
|
|
|
|
|
archivePath.FullName.TrimEnd(Path.DirectorySeparatorChar),
|
|
|
|
|
FileMode.Open,
|
|
|
|
|
FileAccess.Read);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
PartitionFileSystem nsp = new PartitionFileSystem(pfsFile.AsStorage());
|
|
|
|
|
|
|
|
|
|
ImportTitleKeysFromNsp(nsp, context.Device.System.KeySet);
|
2019-10-17 06:17:44 +00:00
|
|
|
|
|
2019-09-08 21:33:40 +00:00
|
|
|
|
string filename = fullPath.Replace(archivePath.FullName, string.Empty).TrimStart('\\');
|
|
|
|
|
|
2021-12-23 16:55:50 +00:00
|
|
|
|
using var ncaFile = new UniqueRef<LibHac.Fs.Fsa.IFile>();
|
|
|
|
|
|
|
|
|
|
Result result = nsp.OpenFile(ref ncaFile.Ref(), filename.ToU8Span(), OpenMode.Read);
|
2019-10-17 06:17:44 +00:00
|
|
|
|
if (result.IsFailure())
|
2019-09-08 21:33:40 +00:00
|
|
|
|
{
|
2019-10-17 06:17:44 +00:00
|
|
|
|
return (ResultCode)result.Value;
|
2019-09-08 21:33:40 +00:00
|
|
|
|
}
|
2019-10-17 06:17:44 +00:00
|
|
|
|
|
2021-12-23 16:55:50 +00:00
|
|
|
|
return OpenNcaFs(context, fullPath, ncaFile.Release().AsStorage(), out openedFileSystem);
|
2019-09-08 21:33:40 +00:00
|
|
|
|
}
|
|
|
|
|
catch (HorizonResultException ex)
|
|
|
|
|
{
|
|
|
|
|
return (ResultCode)ex.ResultValue.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResultCode.PathDoesNotExist;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 21:56:24 +00:00
|
|
|
|
public static void ImportTitleKeysFromNsp(LibHac.Fs.Fsa.IFileSystem nsp, KeySet keySet)
|
2019-09-08 21:33:40 +00:00
|
|
|
|
{
|
2019-10-17 06:17:44 +00:00
|
|
|
|
foreach (DirectoryEntryEx ticketEntry in nsp.EnumerateEntries("/", "*.tik"))
|
2019-09-08 21:33:40 +00:00
|
|
|
|
{
|
2021-12-23 16:55:50 +00:00
|
|
|
|
using var ticketFile = new UniqueRef<LibHac.Fs.Fsa.IFile>();
|
|
|
|
|
|
|
|
|
|
Result result = nsp.OpenFile(ref ticketFile.Ref(), ticketEntry.FullPath.ToU8Span(), OpenMode.Read);
|
2019-09-08 21:33:40 +00:00
|
|
|
|
|
2019-10-17 06:17:44 +00:00
|
|
|
|
if (result.IsSuccess())
|
2019-09-08 21:33:40 +00:00
|
|
|
|
{
|
2021-12-23 16:55:50 +00:00
|
|
|
|
Ticket ticket = new Ticket(ticketFile.Get.AsStream());
|
2019-10-17 06:17:44 +00:00
|
|
|
|
|
|
|
|
|
keySet.ExternalKeySet.Add(new RightsId(ticket.RightsId), new AccessKey(ticket.GetTitleKey(keySet)));
|
2019-09-08 21:33:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-05 11:49:44 +00:00
|
|
|
|
|
|
|
|
|
public static Result ReadFsPath(out FsPath path, ServiceCtx context, int index = 0)
|
|
|
|
|
{
|
2021-04-24 10:16:01 +00:00
|
|
|
|
ulong position = context.Request.PtrBuff[index].Position;
|
|
|
|
|
ulong size = context.Request.PtrBuff[index].Size;
|
2020-01-05 11:49:44 +00:00
|
|
|
|
|
2020-05-03 22:54:50 +00:00
|
|
|
|
byte[] pathBytes = new byte[size];
|
|
|
|
|
|
2021-04-24 10:16:01 +00:00
|
|
|
|
context.Memory.Read(position, pathBytes);
|
2020-01-05 11:49:44 +00:00
|
|
|
|
|
|
|
|
|
return FsPath.FromSpan(out path, pathBytes);
|
|
|
|
|
}
|
2021-08-12 21:56:24 +00:00
|
|
|
|
|
|
|
|
|
public static ref readonly FspPath GetFspPath(ServiceCtx context, int index = 0)
|
|
|
|
|
{
|
|
|
|
|
ulong position = (ulong)context.Request.PtrBuff[index].Position;
|
|
|
|
|
ulong size = (ulong)context.Request.PtrBuff[index].Size;
|
|
|
|
|
|
|
|
|
|
ReadOnlySpan<byte> buffer = context.Memory.GetSpan(position, (int)size);
|
|
|
|
|
ReadOnlySpan<FspPath> fspBuffer = MemoryMarshal.Cast<byte, FspPath>(buffer);
|
|
|
|
|
|
|
|
|
|
return ref fspBuffer[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ref readonly LibHac.FsSrv.Sf.Path GetSfPath(ServiceCtx context, int index = 0)
|
|
|
|
|
{
|
|
|
|
|
ulong position = (ulong)context.Request.PtrBuff[index].Position;
|
|
|
|
|
ulong size = (ulong)context.Request.PtrBuff[index].Size;
|
|
|
|
|
|
|
|
|
|
ReadOnlySpan<byte> buffer = context.Memory.GetSpan(position, (int)size);
|
|
|
|
|
ReadOnlySpan<LibHac.FsSrv.Sf.Path> pathBuffer = MemoryMarshal.Cast<byte, LibHac.FsSrv.Sf.Path>(buffer);
|
|
|
|
|
|
|
|
|
|
return ref pathBuffer[0];
|
|
|
|
|
}
|
2019-09-08 21:33:40 +00:00
|
|
|
|
}
|
2019-10-17 06:17:44 +00:00
|
|
|
|
}
|