2019-09-08 23:33:40 +02:00
|
|
|
|
using LibHac;
|
2020-03-25 09:14:35 +01:00
|
|
|
|
using LibHac.Common;
|
2021-08-12 23:56:24 +02:00
|
|
|
|
using LibHac.Common.Keys;
|
2019-09-08 23:33:40 +02:00
|
|
|
|
using LibHac.Fs;
|
2021-08-12 23:56:24 +02:00
|
|
|
|
using LibHac.FsSrv.Impl;
|
|
|
|
|
using LibHac.FsSrv.Sf;
|
2019-10-17 08:17:44 +02:00
|
|
|
|
using LibHac.FsSystem;
|
|
|
|
|
using LibHac.Spl;
|
2022-01-12 12:22:19 +01:00
|
|
|
|
using LibHac.Tools.Es;
|
|
|
|
|
using LibHac.Tools.Fs;
|
|
|
|
|
using LibHac.Tools.FsSystem;
|
|
|
|
|
using LibHac.Tools.FsSystem.NcaUtils;
|
2021-08-12 23:56:24 +02:00
|
|
|
|
using System;
|
2019-09-08 23:33:40 +02:00
|
|
|
|
using System.IO;
|
2021-08-12 23:56:24 +02:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using Path = System.IO.Path;
|
2019-09-08 23:33:40 +02:00
|
|
|
|
|
2019-09-19 02:45:11 +02:00
|
|
|
|
namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy
|
2019-09-08 23:33:40 +02:00
|
|
|
|
{
|
2019-09-19 02:45:11 +02:00
|
|
|
|
static class FileSystemProxyHelper
|
2019-09-08 23:33:40 +02:00
|
|
|
|
{
|
|
|
|
|
public static ResultCode OpenNsp(ServiceCtx context, string pfsPath, out IFileSystem openedFileSystem)
|
|
|
|
|
{
|
|
|
|
|
openedFileSystem = null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-08-12 23:56:24 +02:00
|
|
|
|
LocalStorage storage = new LocalStorage(pfsPath, FileAccess.Read, FileMode.Open);
|
2021-12-23 17:55:50 +01:00
|
|
|
|
using SharedRef<LibHac.Fs.Fsa.IFileSystem> nsp = new(new PartitionFileSystem(storage));
|
2019-09-08 23:33:40 +02:00
|
|
|
|
|
2021-12-23 17:55:50 +01:00
|
|
|
|
ImportTitleKeysFromNsp(nsp.Get, context.Device.System.KeySet);
|
2019-09-08 23:33:40 +02:00
|
|
|
|
|
2023-03-02 03:42:27 +01:00
|
|
|
|
using SharedRef<LibHac.FsSrv.Sf.IFileSystem> adapter = FileSystemInterfaceAdapter.CreateShared(ref nsp.Ref, true);
|
2021-12-23 17:55:50 +01:00
|
|
|
|
|
2023-03-02 03:42:27 +01:00
|
|
|
|
openedFileSystem = new IFileSystem(ref adapter.Ref);
|
2019-09-08 23:33:40 +02: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 22:08:59 +02:00
|
|
|
|
LibHac.Fs.Fsa.IFileSystem fileSystem = nca.OpenFileSystem(NcaSectionType.Data, context.Device.System.FsIntegrityCheckLevel);
|
2021-12-23 17:55:50 +01:00
|
|
|
|
using var sharedFs = new SharedRef<LibHac.Fs.Fsa.IFileSystem>(fileSystem);
|
|
|
|
|
|
2023-03-02 03:42:27 +01:00
|
|
|
|
using SharedRef<LibHac.FsSrv.Sf.IFileSystem> adapter = FileSystemInterfaceAdapter.CreateShared(ref sharedFs.Ref, true);
|
2019-09-08 23:33:40 +02:00
|
|
|
|
|
2023-03-02 03:42:27 +01:00
|
|
|
|
openedFileSystem = new IFileSystem(ref adapter.Ref);
|
2019-09-08 23:33:40 +02: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 08:17:44 +02:00
|
|
|
|
|
2019-09-08 23:33:40 +02:00
|
|
|
|
string filename = fullPath.Replace(archivePath.FullName, string.Empty).TrimStart('\\');
|
|
|
|
|
|
2021-12-23 17:55:50 +01:00
|
|
|
|
using var ncaFile = new UniqueRef<LibHac.Fs.Fsa.IFile>();
|
|
|
|
|
|
2023-03-02 03:42:27 +01:00
|
|
|
|
Result result = nsp.OpenFile(ref ncaFile.Ref, filename.ToU8Span(), OpenMode.Read);
|
2019-10-17 08:17:44 +02:00
|
|
|
|
if (result.IsFailure())
|
2019-09-08 23:33:40 +02:00
|
|
|
|
{
|
2019-10-17 08:17:44 +02:00
|
|
|
|
return (ResultCode)result.Value;
|
2019-09-08 23:33:40 +02:00
|
|
|
|
}
|
2019-10-17 08:17:44 +02:00
|
|
|
|
|
2021-12-23 17:55:50 +01:00
|
|
|
|
return OpenNcaFs(context, fullPath, ncaFile.Release().AsStorage(), out openedFileSystem);
|
2019-09-08 23:33:40 +02:00
|
|
|
|
}
|
|
|
|
|
catch (HorizonResultException ex)
|
|
|
|
|
{
|
|
|
|
|
return (ResultCode)ex.ResultValue.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResultCode.PathDoesNotExist;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 23:56:24 +02:00
|
|
|
|
public static void ImportTitleKeysFromNsp(LibHac.Fs.Fsa.IFileSystem nsp, KeySet keySet)
|
2019-09-08 23:33:40 +02:00
|
|
|
|
{
|
2019-10-17 08:17:44 +02:00
|
|
|
|
foreach (DirectoryEntryEx ticketEntry in nsp.EnumerateEntries("/", "*.tik"))
|
2019-09-08 23:33:40 +02:00
|
|
|
|
{
|
2021-12-23 17:55:50 +01:00
|
|
|
|
using var ticketFile = new UniqueRef<LibHac.Fs.Fsa.IFile>();
|
|
|
|
|
|
2023-03-02 03:42:27 +01:00
|
|
|
|
Result result = nsp.OpenFile(ref ticketFile.Ref, ticketEntry.FullPath.ToU8Span(), OpenMode.Read);
|
2019-09-08 23:33:40 +02:00
|
|
|
|
|
2019-10-17 08:17:44 +02:00
|
|
|
|
if (result.IsSuccess())
|
2019-09-08 23:33:40 +02:00
|
|
|
|
{
|
2021-12-23 17:55:50 +01:00
|
|
|
|
Ticket ticket = new Ticket(ticketFile.Get.AsStream());
|
2023-03-02 03:42:27 +01:00
|
|
|
|
var titleKey = ticket.GetTitleKey(keySet);
|
2019-10-17 08:17:44 +02:00
|
|
|
|
|
2023-03-02 03:42:27 +01:00
|
|
|
|
if (titleKey != null)
|
|
|
|
|
{
|
|
|
|
|
keySet.ExternalKeySet.Add(new RightsId(ticket.RightsId), new AccessKey(titleKey));
|
|
|
|
|
}
|
2019-09-08 23:33:40 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-05 12:49:44 +01:00
|
|
|
|
|
2021-08-12 23:56:24 +02:00
|
|
|
|
public static ref readonly FspPath GetFspPath(ServiceCtx context, int index = 0)
|
|
|
|
|
{
|
2022-02-27 00:52:25 +01:00
|
|
|
|
ulong position = context.Request.PtrBuff[index].Position;
|
|
|
|
|
ulong size = context.Request.PtrBuff[index].Size;
|
2021-08-12 23:56:24 +02:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2022-02-27 00:52:25 +01:00
|
|
|
|
ulong position = context.Request.PtrBuff[index].Position;
|
|
|
|
|
ulong size = context.Request.PtrBuff[index].Size;
|
2021-08-12 23:56:24 +02:00
|
|
|
|
|
|
|
|
|
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 23:33:40 +02:00
|
|
|
|
}
|
2019-10-17 08:17:44 +02:00
|
|
|
|
}
|