2019-09-08 23:33:40 +02:00
|
|
|
|
using LibHac;
|
2020-03-25 09:14:35 +01:00
|
|
|
|
using LibHac.Common;
|
2019-09-08 23:33:40 +02:00
|
|
|
|
using LibHac.Fs;
|
2019-10-17 08:17:44 +02:00
|
|
|
|
using LibHac.FsSystem;
|
|
|
|
|
using LibHac.FsSystem.NcaUtils;
|
|
|
|
|
using LibHac.Spl;
|
2019-09-08 23:33:40 +02:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
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-07-13 16:48:54 +02:00
|
|
|
|
LocalStorage storage = new LocalStorage(pfsPath, FileAccess.Read, FileMode.Open);
|
|
|
|
|
PartitionFileSystem nsp = new PartitionFileSystem(storage);
|
2019-09-08 23:33:40 +02:00
|
|
|
|
|
2021-07-13 16:48:54 +02:00
|
|
|
|
ImportTitleKeysFromNsp(nsp, context.Device.System.KeySet);
|
2019-09-08 23:33:40 +02:00
|
|
|
|
|
2021-07-13 16:48:54 +02:00
|
|
|
|
openedFileSystem = new IFileSystem(nsp);
|
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);
|
2019-09-08 23:33:40 +02:00
|
|
|
|
|
2021-07-13 16:48:54 +02:00
|
|
|
|
openedFileSystem = new IFileSystem(fileSystem);
|
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('\\');
|
|
|
|
|
|
2020-09-01 22:08:59 +02:00
|
|
|
|
Result result = nsp.OpenFile(out LibHac.Fs.Fsa.IFile ncaFile, 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
|
|
|
|
|
|
|
|
|
return OpenNcaFs(context, fullPath, ncaFile.AsStorage(), out openedFileSystem);
|
2019-09-08 23:33:40 +02:00
|
|
|
|
}
|
|
|
|
|
catch (HorizonResultException ex)
|
|
|
|
|
{
|
|
|
|
|
return (ResultCode)ex.ResultValue.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResultCode.PathDoesNotExist;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-13 16:48:54 +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
|
|
|
|
{
|
2020-09-01 22:08:59 +02:00
|
|
|
|
Result result = nsp.OpenFile(out LibHac.Fs.Fsa.IFile ticketFile, 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
|
|
|
|
{
|
2019-10-17 08:17:44 +02:00
|
|
|
|
Ticket ticket = new Ticket(ticketFile.AsStream());
|
|
|
|
|
|
|
|
|
|
keySet.ExternalKeySet.Add(new RightsId(ticket.RightsId), new AccessKey(ticket.GetTitleKey(keySet)));
|
2019-09-08 23:33:40 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-05 12:49:44 +01:00
|
|
|
|
|
|
|
|
|
public static Result ReadFsPath(out FsPath path, ServiceCtx context, int index = 0)
|
|
|
|
|
{
|
2021-04-24 12:16:01 +02:00
|
|
|
|
ulong position = context.Request.PtrBuff[index].Position;
|
|
|
|
|
ulong size = context.Request.PtrBuff[index].Size;
|
2020-01-05 12:49:44 +01:00
|
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
|
byte[] pathBytes = new byte[size];
|
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
|
context.Memory.Read(position, pathBytes);
|
2020-01-05 12:49:44 +01:00
|
|
|
|
|
|
|
|
|
return FsPath.FromSpan(out path, pathBytes);
|
|
|
|
|
}
|
2019-09-08 23:33:40 +02:00
|
|
|
|
}
|
2019-10-17 08:17:44 +02:00
|
|
|
|
}
|