aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Services/Fs
diff options
context:
space:
mode:
authorAc_K <Acoustik666@gmail.com>2019-09-19 02:45:11 +0200
committerjduncanator <1518948+jduncanator@users.noreply.github.com>2019-09-19 10:45:11 +1000
commita0720b5681852f3d786d77bd3793b0359dea321c (patch)
tree9d8f61e540d1d1d827999902dad95e5c0c1e076e /Ryujinx.HLE/HOS/Services/Fs
parent4af3101b22e6957d6aa48a2768566d658699f4ed (diff)
Refactoring HOS folder structure (#771)
* Refactoring HOS folder structure Refactoring HOS folder structure: - Added some subfolders when needed (Following structure decided in private). - Added some `Types` folders when needed. - Little cleanup here and there. - Add services placeholders for every HOS services (close #766 and #753). * Remove Types namespaces
Diffstat (limited to 'Ryujinx.HLE/HOS/Services/Fs')
-rw-r--r--Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/FileSystemProxyHelper.cs144
-rw-r--r--Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IDirectory.cs84
-rw-r--r--Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IFile.cs136
-rw-r--r--Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IFileSystem.cs324
-rw-r--r--Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IStorage.cs65
-rw-r--r--Ryujinx.HLE/HOS/Services/Fs/IFileSystemProxy.cs272
-rw-r--r--Ryujinx.HLE/HOS/Services/Fs/IFileSystemProxyForLoader.cs8
-rw-r--r--Ryujinx.HLE/HOS/Services/Fs/IProgramRegistry.cs8
-rw-r--r--Ryujinx.HLE/HOS/Services/Fs/ResultCode.cs16
-rw-r--r--Ryujinx.HLE/HOS/Services/Fs/Types/FileSystemType.cs12
10 files changed, 1069 insertions, 0 deletions
diff --git a/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/FileSystemProxyHelper.cs b/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/FileSystemProxyHelper.cs
new file mode 100644
index 00000000..04b87b57
--- /dev/null
+++ b/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/FileSystemProxyHelper.cs
@@ -0,0 +1,144 @@
+using LibHac;
+using LibHac.Fs;
+using LibHac.Fs.NcaUtils;
+using Ryujinx.Common;
+using Ryujinx.HLE.FileSystem;
+using Ryujinx.HLE.Utilities;
+using System.IO;
+
+namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy
+{
+ static class FileSystemProxyHelper
+ {
+ public static ResultCode LoadSaveDataFileSystem(ServiceCtx context, bool readOnly, out IFileSystem loadedFileSystem)
+ {
+ loadedFileSystem = null;
+
+ SaveSpaceId saveSpaceId = (SaveSpaceId)context.RequestData.ReadInt64();
+ ulong titleId = context.RequestData.ReadUInt64();
+ UInt128 userId = context.RequestData.ReadStruct<UInt128>();
+ long saveId = context.RequestData.ReadInt64();
+ SaveDataType saveDataType = (SaveDataType)context.RequestData.ReadByte();
+ SaveInfo saveInfo = new SaveInfo(titleId, saveId, saveDataType, saveSpaceId, userId);
+ string savePath = context.Device.FileSystem.GetSavePath(context, saveInfo);
+
+ try
+ {
+ LocalFileSystem fileSystem = new LocalFileSystem(savePath);
+ LibHac.Fs.IFileSystem saveFileSystem = new DirectorySaveDataFileSystem(fileSystem);
+
+ if (readOnly)
+ {
+ saveFileSystem = new ReadOnlyFileSystem(saveFileSystem);
+ }
+
+ loadedFileSystem = new IFileSystem(saveFileSystem);
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ return ResultCode.Success;
+ }
+
+ public static ResultCode OpenNsp(ServiceCtx context, string pfsPath, out IFileSystem openedFileSystem)
+ {
+ openedFileSystem = null;
+
+ try
+ {
+ LocalStorage storage = new LocalStorage(pfsPath, FileAccess.Read, FileMode.Open);
+ PartitionFileSystem nsp = new PartitionFileSystem(storage);
+
+ ImportTitleKeysFromNsp(nsp, context.Device.System.KeySet);
+
+ openedFileSystem = new IFileSystem(nsp);
+ }
+ 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;
+ }
+
+ LibHac.Fs.IFileSystem fileSystem = nca.OpenFileSystem(NcaSectionType.Data, context.Device.System.FsIntegrityCheckLevel);
+
+ openedFileSystem = new IFileSystem(fileSystem);
+ }
+ 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);
+
+ string filename = fullPath.Replace(archivePath.FullName, string.Empty).TrimStart('\\');
+
+ if (nsp.FileExists(filename))
+ {
+ return OpenNcaFs(context, fullPath, nsp.OpenFile(filename, OpenMode.Read).AsStorage(), out openedFileSystem);
+ }
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+ }
+
+ return ResultCode.PathDoesNotExist;
+ }
+
+ public static void ImportTitleKeysFromNsp(LibHac.Fs.IFileSystem nsp, Keyset keySet)
+ {
+ foreach (DirectoryEntry ticketEntry in nsp.EnumerateEntries("*.tik"))
+ {
+ Ticket ticket = new Ticket(nsp.OpenFile(ticketEntry.FullPath, OpenMode.Read).AsStream());
+
+ if (!keySet.TitleKeys.ContainsKey(ticket.RightsId))
+ {
+ keySet.TitleKeys.Add(ticket.RightsId, ticket.GetTitleKey(keySet));
+ }
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IDirectory.cs b/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IDirectory.cs
new file mode 100644
index 00000000..4fc8a687
--- /dev/null
+++ b/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IDirectory.cs
@@ -0,0 +1,84 @@
+using LibHac;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy
+{
+ class IDirectory : IpcService
+ {
+ private const int DirectoryEntrySize = 0x310;
+
+ private IEnumerator<LibHac.Fs.DirectoryEntry> _enumerator;
+
+ private LibHac.Fs.IDirectory _baseDirectory;
+
+ public IDirectory(LibHac.Fs.IDirectory directory)
+ {
+ _baseDirectory = directory;
+ _enumerator = directory.Read().GetEnumerator();
+ }
+
+ [Command(0)]
+ // Read() -> (u64 count, buffer<nn::fssrv::sf::IDirectoryEntry, 6, 0> entries)
+ public ResultCode Read(ServiceCtx context)
+ {
+ long bufferPosition = context.Request.ReceiveBuff[0].Position;
+ long bufferLen = context.Request.ReceiveBuff[0].Size;
+
+ int maxReadCount = (int)(bufferLen / DirectoryEntrySize);
+ int readCount = 0;
+
+ try
+ {
+ while (readCount < maxReadCount && _enumerator.MoveNext())
+ {
+ long position = bufferPosition + readCount * DirectoryEntrySize;
+
+ WriteDirectoryEntry(context, position, _enumerator.Current);
+
+ readCount++;
+ }
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ context.ResponseData.Write((long)readCount);
+
+ return ResultCode.Success;
+ }
+
+ private void WriteDirectoryEntry(ServiceCtx context, long position, LibHac.Fs.DirectoryEntry entry)
+ {
+ for (int offset = 0; offset < 0x300; offset += 8)
+ {
+ context.Memory.WriteInt64(position + offset, 0);
+ }
+
+ byte[] nameBuffer = Encoding.UTF8.GetBytes(entry.Name);
+
+ context.Memory.WriteBytes(position, nameBuffer);
+
+ context.Memory.WriteInt32(position + 0x300, (int)entry.Attributes);
+ context.Memory.WriteInt32(position + 0x304, (byte)entry.Type);
+ context.Memory.WriteInt64(position + 0x308, entry.Size);
+ }
+
+ [Command(1)]
+ // GetEntryCount() -> u64
+ public ResultCode GetEntryCount(ServiceCtx context)
+ {
+ try
+ {
+ context.ResponseData.Write((long)_baseDirectory.GetEntryCount());
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ return ResultCode.Success;
+ }
+ }
+}
diff --git a/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IFile.cs b/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IFile.cs
new file mode 100644
index 00000000..df9209e6
--- /dev/null
+++ b/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IFile.cs
@@ -0,0 +1,136 @@
+using LibHac;
+using LibHac.Fs;
+using System;
+
+namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy
+{
+ class IFile : IpcService, IDisposable
+ {
+ private LibHac.Fs.IFile _baseFile;
+
+ public IFile(LibHac.Fs.IFile baseFile)
+ {
+ _baseFile = baseFile;
+ }
+
+ [Command(0)]
+ // Read(u32 readOption, u64 offset, u64 size) -> (u64 out_size, buffer<u8, 0x46, 0> out_buf)
+ public ResultCode Read(ServiceCtx context)
+ {
+ long position = context.Request.ReceiveBuff[0].Position;
+
+ ReadOption readOption = (ReadOption)context.RequestData.ReadInt32();
+ context.RequestData.BaseStream.Position += 4;
+
+ long offset = context.RequestData.ReadInt64();
+ long size = context.RequestData.ReadInt64();
+
+ byte[] data = new byte[size];
+ int readSize;
+
+ try
+ {
+ readSize = _baseFile.Read(data, offset, readOption);
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ context.Memory.WriteBytes(position, data);
+
+ context.ResponseData.Write((long)readSize);
+
+ return ResultCode.Success;
+ }
+
+ [Command(1)]
+ // Write(u32 writeOption, u64 offset, u64 size, buffer<u8, 0x45, 0>)
+ public ResultCode Write(ServiceCtx context)
+ {
+ long position = context.Request.SendBuff[0].Position;
+
+ WriteOption writeOption = (WriteOption)context.RequestData.ReadInt32();
+ context.RequestData.BaseStream.Position += 4;
+
+ long offset = context.RequestData.ReadInt64();
+ long size = context.RequestData.ReadInt64();
+
+ byte[] data = context.Memory.ReadBytes(position, size);
+
+ try
+ {
+ _baseFile.Write(data, offset, writeOption);
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ return ResultCode.Success;
+ }
+
+ [Command(2)]
+ // Flush()
+ public ResultCode Flush(ServiceCtx context)
+ {
+ try
+ {
+ _baseFile.Flush();
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ return ResultCode.Success;
+ }
+
+ [Command(3)]
+ // SetSize(u64 size)
+ public ResultCode SetSize(ServiceCtx context)
+ {
+ try
+ {
+ long size = context.RequestData.ReadInt64();
+
+ _baseFile.SetSize(size);
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ return ResultCode.Success;
+ }
+
+ [Command(4)]
+ // GetSize() -> u64 fileSize
+ public ResultCode GetSize(ServiceCtx context)
+ {
+ try
+ {
+ context.ResponseData.Write(_baseFile.GetSize());
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ return ResultCode.Success;
+ }
+
+ public void Dispose()
+ {
+ Dispose(true);
+ }
+
+ protected virtual void Dispose(bool disposing)
+ {
+ if (disposing)
+ {
+ _baseFile?.Dispose();
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IFileSystem.cs b/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IFileSystem.cs
new file mode 100644
index 00000000..7a7fdbaf
--- /dev/null
+++ b/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IFileSystem.cs
@@ -0,0 +1,324 @@
+using LibHac;
+using LibHac.Fs;
+
+using static Ryujinx.HLE.Utilities.StringUtils;
+
+namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy
+{
+ class IFileSystem : IpcService
+ {
+ private LibHac.Fs.IFileSystem _fileSystem;
+
+ public IFileSystem(LibHac.Fs.IFileSystem provider)
+ {
+ _fileSystem = provider;
+ }
+
+ [Command(0)]
+ // CreateFile(u32 createOption, u64 size, buffer<bytes<0x301>, 0x19, 0x301> path)
+ public ResultCode CreateFile(ServiceCtx context)
+ {
+ string name = ReadUtf8String(context);
+
+ CreateFileOptions createOption = (CreateFileOptions)context.RequestData.ReadInt32();
+ context.RequestData.BaseStream.Position += 4;
+
+ long size = context.RequestData.ReadInt64();
+
+ try
+ {
+ _fileSystem.CreateFile(name, size, createOption);
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ return ResultCode.Success;
+ }
+
+ [Command(1)]
+ // DeleteFile(buffer<bytes<0x301>, 0x19, 0x301> path)
+ public ResultCode DeleteFile(ServiceCtx context)
+ {
+ string name = ReadUtf8String(context);
+
+ try
+ {
+ _fileSystem.DeleteFile(name);
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ return ResultCode.Success;
+ }
+
+ [Command(2)]
+ // CreateDirectory(buffer<bytes<0x301>, 0x19, 0x301> path)
+ public ResultCode CreateDirectory(ServiceCtx context)
+ {
+ string name = ReadUtf8String(context);
+
+ try
+ {
+ _fileSystem.CreateDirectory(name);
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ return ResultCode.Success;
+ }
+
+ [Command(3)]
+ // DeleteDirectory(buffer<bytes<0x301>, 0x19, 0x301> path)
+ public ResultCode DeleteDirectory(ServiceCtx context)
+ {
+ string name = ReadUtf8String(context);
+
+ try
+ {
+ _fileSystem.DeleteDirectory(name);
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ return ResultCode.Success;
+ }
+
+ [Command(4)]
+ // DeleteDirectoryRecursively(buffer<bytes<0x301>, 0x19, 0x301> path)
+ public ResultCode DeleteDirectoryRecursively(ServiceCtx context)
+ {
+ string name = ReadUtf8String(context);
+
+ try
+ {
+ _fileSystem.DeleteDirectoryRecursively(name);
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ return ResultCode.Success;
+ }
+
+ [Command(5)]
+ // RenameFile(buffer<bytes<0x301>, 0x19, 0x301> oldPath, buffer<bytes<0x301>, 0x19, 0x301> newPath)
+ public ResultCode RenameFile(ServiceCtx context)
+ {
+ string oldName = ReadUtf8String(context, 0);
+ string newName = ReadUtf8String(context, 1);
+
+ try
+ {
+ _fileSystem.RenameFile(oldName, newName);
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ return ResultCode.Success;
+ }
+
+ [Command(6)]
+ // RenameDirectory(buffer<bytes<0x301>, 0x19, 0x301> oldPath, buffer<bytes<0x301>, 0x19, 0x301> newPath)
+ public ResultCode RenameDirectory(ServiceCtx context)
+ {
+ string oldName = ReadUtf8String(context, 0);
+ string newName = ReadUtf8String(context, 1);
+
+ try
+ {
+ _fileSystem.RenameDirectory(oldName, newName);
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ return ResultCode.Success;
+ }
+
+ [Command(7)]
+ // GetEntryType(buffer<bytes<0x301>, 0x19, 0x301> path) -> nn::fssrv::sf::DirectoryEntryType
+ public ResultCode GetEntryType(ServiceCtx context)
+ {
+ string name = ReadUtf8String(context);
+
+ try
+ {
+ DirectoryEntryType entryType = _fileSystem.GetEntryType(name);
+
+ if (entryType == DirectoryEntryType.Directory || entryType == DirectoryEntryType.File)
+ {
+ context.ResponseData.Write((int)entryType);
+ }
+ else
+ {
+ return ResultCode.PathDoesNotExist;
+ }
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ return ResultCode.Success;
+ }
+
+ [Command(8)]
+ // OpenFile(u32 mode, buffer<bytes<0x301>, 0x19, 0x301> path) -> object<nn::fssrv::sf::IFile> file
+ public ResultCode OpenFile(ServiceCtx context)
+ {
+ OpenMode mode = (OpenMode)context.RequestData.ReadInt32();
+
+ string name = ReadUtf8String(context);
+
+ try
+ {
+ LibHac.Fs.IFile file = _fileSystem.OpenFile(name, mode);
+
+ IFile fileInterface = new IFile(file);
+
+ MakeObject(context, fileInterface);
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ return ResultCode.Success;
+ }
+
+ [Command(9)]
+ // OpenDirectory(u32 filter_flags, buffer<bytes<0x301>, 0x19, 0x301> path) -> object<nn::fssrv::sf::IDirectory> directory
+ public ResultCode OpenDirectory(ServiceCtx context)
+ {
+ OpenDirectoryMode mode = (OpenDirectoryMode)context.RequestData.ReadInt32();
+
+ string name = ReadUtf8String(context);
+
+ try
+ {
+ LibHac.Fs.IDirectory dir = _fileSystem.OpenDirectory(name, mode);
+
+ IDirectory dirInterface = new IDirectory(dir);
+
+ MakeObject(context, dirInterface);
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ return ResultCode.Success;
+ }
+
+ [Command(10)]
+ // Commit()
+ public ResultCode Commit(ServiceCtx context)
+ {
+ try
+ {
+ _fileSystem.Commit();
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ return ResultCode.Success;
+ }
+
+ [Command(11)]
+ // GetFreeSpaceSize(buffer<bytes<0x301>, 0x19, 0x301> path) -> u64 totalFreeSpace
+ public ResultCode GetFreeSpaceSize(ServiceCtx context)
+ {
+ string name = ReadUtf8String(context);
+
+ try
+ {
+ context.ResponseData.Write(_fileSystem.GetFreeSpaceSize(name));
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ return ResultCode.Success;
+ }
+
+ [Command(12)]
+ // GetTotalSpaceSize(buffer<bytes<0x301>, 0x19, 0x301> path) -> u64 totalSize
+ public ResultCode GetTotalSpaceSize(ServiceCtx context)
+ {
+ string name = ReadUtf8String(context);
+
+ try
+ {
+ context.ResponseData.Write(_fileSystem.GetTotalSpaceSize(name));
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ return ResultCode.Success;
+ }
+
+ [Command(13)]
+ // CleanDirectoryRecursively(buffer<bytes<0x301>, 0x19, 0x301> path)
+ public ResultCode CleanDirectoryRecursively(ServiceCtx context)
+ {
+ string name = ReadUtf8String(context);
+
+ try
+ {
+ _fileSystem.CleanDirectoryRecursively(name);
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ return ResultCode.Success;
+ }
+
+ [Command(14)]
+ // GetFileTimeStampRaw(buffer<bytes<0x301>, 0x19, 0x301> path) -> bytes<0x20> timestamp
+ public ResultCode GetFileTimeStampRaw(ServiceCtx context)
+ {
+ string name = ReadUtf8String(context);
+
+ try
+ {
+ FileTimeStampRaw timestamp = _fileSystem.GetFileTimeStampRaw(name);
+
+ context.ResponseData.Write(timestamp.Created);
+ context.ResponseData.Write(timestamp.Modified);
+ context.ResponseData.Write(timestamp.Accessed);
+
+ byte[] data = new byte[8];
+
+ // is valid?
+ data[0] = 1;
+
+ context.ResponseData.Write(data);
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ return ResultCode.Success;
+ }
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IStorage.cs b/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IStorage.cs
new file mode 100644
index 00000000..107599a4
--- /dev/null
+++ b/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IStorage.cs
@@ -0,0 +1,65 @@
+using LibHac;
+using Ryujinx.HLE.HOS.Ipc;
+
+namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy
+{
+ class IStorage : IpcService
+ {
+ private LibHac.Fs.IStorage _baseStorage;
+
+ public IStorage(LibHac.Fs.IStorage baseStorage)
+ {
+ _baseStorage = baseStorage;
+ }
+
+ [Command(0)]
+ // Read(u64 offset, u64 length) -> buffer<u8, 0x46, 0> buffer
+ public ResultCode Read(ServiceCtx context)
+ {
+ long offset = context.RequestData.ReadInt64();
+ long size = context.RequestData.ReadInt64();
+
+ if (context.Request.ReceiveBuff.Count > 0)
+ {
+ IpcBuffDesc buffDesc = context.Request.ReceiveBuff[0];
+
+ // Use smaller length to avoid overflows.
+ if (size > buffDesc.Size)
+ {
+ size = buffDesc.Size;
+ }
+
+ byte[] data = new byte[size];
+
+ try
+ {
+ _baseStorage.Read(data, offset);
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ context.Memory.WriteBytes(buffDesc.Position, data);
+ }
+
+ return ResultCode.Success;
+ }
+
+ [Command(4)]
+ // GetSize() -> u64 size
+ public ResultCode GetSize(ServiceCtx context)
+ {
+ try
+ {
+ context.ResponseData.Write(_baseStorage.GetSize());
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ return ResultCode.Success;
+ }
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.HLE/HOS/Services/Fs/IFileSystemProxy.cs b/Ryujinx.HLE/HOS/Services/Fs/IFileSystemProxy.cs
new file mode 100644
index 00000000..a0d22595
--- /dev/null
+++ b/Ryujinx.HLE/HOS/Services/Fs/IFileSystemProxy.cs
@@ -0,0 +1,272 @@
+using LibHac;
+using LibHac.Fs;
+using LibHac.Fs.NcaUtils;
+using Ryujinx.Common.Logging;
+using Ryujinx.HLE.FileSystem;
+using Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy;
+using System.IO;
+
+using static Ryujinx.HLE.FileSystem.VirtualFileSystem;
+using static Ryujinx.HLE.Utilities.StringUtils;
+
+namespace Ryujinx.HLE.HOS.Services.Fs
+{
+ [Service("fsp-srv")]
+ class IFileSystemProxy : IpcService
+ {
+ public IFileSystemProxy(ServiceCtx context) { }
+
+ [Command(1)]
+ // Initialize(u64, pid)
+ public ResultCode Initialize(ServiceCtx context)
+ {
+ return ResultCode.Success;
+ }
+
+ [Command(8)]
+ // OpenFileSystemWithId(nn::fssrv::sf::FileSystemType filesystem_type, nn::ApplicationId tid, buffer<bytes<0x301>, 0x19, 0x301> path)
+ // -> object<nn::fssrv::sf::IFileSystem> contentFs
+ public ResultCode OpenFileSystemWithId(ServiceCtx context)
+ {
+ FileSystemType fileSystemType = (FileSystemType)context.RequestData.ReadInt32();
+ long titleId = context.RequestData.ReadInt64();
+ string switchPath = ReadUtf8String(context);
+ string fullPath = context.Device.FileSystem.SwitchPathToSystemPath(switchPath);
+
+ if (!File.Exists(fullPath))
+ {
+ if (fullPath.Contains("."))
+ {
+ ResultCode result = FileSystemProxyHelper.OpenFileSystemFromInternalFile(context, fullPath, out FileSystemProxy.IFileSystem fileSystem);
+
+ if (result == ResultCode.Success)
+ {
+ MakeObject(context, fileSystem);
+ }
+
+ return result;
+ }
+
+ return ResultCode.PathDoesNotExist;
+ }
+
+ FileStream fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
+ string extension = Path.GetExtension(fullPath);
+
+ if (extension == ".nca")
+ {
+ ResultCode result = FileSystemProxyHelper.OpenNcaFs(context, fullPath, fileStream.AsStorage(), out FileSystemProxy.IFileSystem fileSystem);
+
+ if (result == ResultCode.Success)
+ {
+ MakeObject(context, fileSystem);
+ }
+
+ return result;
+ }
+ else if (extension == ".nsp")
+ {
+ ResultCode result = FileSystemProxyHelper.OpenNsp(context, fullPath, out FileSystemProxy.IFileSystem fileSystem);
+
+ if (result == ResultCode.Success)
+ {
+ MakeObject(context, fileSystem);
+ }
+
+ return result;
+ }
+
+ return ResultCode.InvalidInput;
+ }
+
+ [Command(11)]
+ // OpenBisFileSystem(nn::fssrv::sf::Partition partitionID, buffer<bytes<0x301>, 0x19, 0x301>) -> object<nn::fssrv::sf::IFileSystem> Bis
+ public ResultCode OpenBisFileSystem(ServiceCtx context)
+ {
+ int bisPartitionId = context.RequestData.ReadInt32();
+ string partitionString = ReadUtf8String(context);
+ string bisPartitionPath = string.Empty;
+
+ switch (bisPartitionId)
+ {
+ case 29:
+ bisPartitionPath = SafeNandPath;
+ break;
+ case 30:
+ case 31:
+ bisPartitionPath = SystemNandPath;
+ break;
+ case 32:
+ bisPartitionPath = UserNandPath;
+ break;
+ default:
+ return ResultCode.InvalidInput;
+ }
+
+ string fullPath = context.Device.FileSystem.GetFullPartitionPath(bisPartitionPath);
+
+ LocalFileSystem fileSystem = new LocalFileSystem(fullPath);
+
+ MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
+
+ return ResultCode.Success;
+ }
+
+ [Command(18)]
+ // OpenSdCardFileSystem() -> object<nn::fssrv::sf::IFileSystem>
+ public ResultCode OpenSdCardFileSystem(ServiceCtx context)
+ {
+ string sdCardPath = context.Device.FileSystem.GetSdCardPath();
+
+ LocalFileSystem fileSystem = new LocalFileSystem(sdCardPath);
+
+ MakeObject(context, new FileSystemProxy.IFileSystem(fileSystem));
+
+ return ResultCode.Success;
+ }
+
+ [Command(51)]
+ // OpenSaveDataFileSystem(u8 save_data_space_id, nn::fssrv::sf::SaveStruct saveStruct) -> object<nn::fssrv::sf::IFileSystem> saveDataFs
+ public ResultCode OpenSaveDataFileSystem(ServiceCtx context)
+ {
+ ResultCode result = FileSystemProxyHelper.LoadSaveDataFileSystem(context, false, out FileSystemProxy.IFileSystem fileSystem);
+
+ if (result == ResultCode.Success)
+ {
+ MakeObject(context, fileSystem);
+ }
+
+ return result;
+ }
+
+ [Command(52)]
+ // OpenSaveDataFileSystemBySystemSaveDataId(u8 save_data_space_id, nn::fssrv::sf::SaveStruct saveStruct) -> object<nn::fssrv::sf::IFileSystem> systemSaveDataFs
+ public ResultCode OpenSaveDataFileSystemBySystemSaveDataId(ServiceCtx context)
+ {
+ ResultCode result = FileSystemProxyHelper.LoadSaveDataFileSystem(context, false, out FileSystemProxy.IFileSystem fileSystem);
+
+ if (result == ResultCode.Success)
+ {
+ MakeObject(context, fileSystem);
+ }
+
+ return result;
+ }
+
+ [Command(53)]
+ // OpenReadOnlySaveDataFileSystem(u8 save_data_space_id, nn::fssrv::sf::SaveStruct save_struct) -> object<nn::fssrv::sf::IFileSystem>
+ public ResultCode OpenReadOnlySaveDataFileSystem(ServiceCtx context)
+ {
+ ResultCode result = FileSystemProxyHelper.LoadSaveDataFileSystem(context, true, out FileSystemProxy.IFileSystem fileSystem);
+
+ if (result == ResultCode.Success)
+ {
+ MakeObject(context, fileSystem);
+ }
+
+ return result;
+ }
+
+ [Command(200)]
+ // OpenDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage> dataStorage
+ public ResultCode OpenDataStorageByCurrentProcess(ServiceCtx context)
+ {
+ MakeObject(context, new FileSystemProxy.IStorage(context.Device.FileSystem.RomFs.AsStorage()));
+
+ return 0;
+ }
+
+ [Command(202)]
+ // OpenDataStorageByDataId(u8 storageId, nn::ApplicationId tid) -> object<nn::fssrv::sf::IStorage> dataStorage
+ public ResultCode OpenDataStorageByDataId(ServiceCtx context)
+ {
+ StorageId storageId = (StorageId)context.RequestData.ReadByte();
+ byte[] padding = context.RequestData.ReadBytes(7);
+ long titleId = context.RequestData.ReadInt64();
+
+ ContentType contentType = ContentType.Data;
+
+ StorageId installedStorage =
+ context.Device.System.ContentManager.GetInstalledStorage(titleId, contentType, storageId);
+
+ if (installedStorage == StorageId.None)
+ {
+ contentType = ContentType.PublicData;
+
+ installedStorage =
+ context.Device.System.ContentManager.GetInstalledStorage(titleId, contentType, storageId);
+ }
+
+ if (installedStorage != StorageId.None)
+ {
+ string contentPath = context.Device.System.ContentManager.GetInstalledContentPath(titleId, storageId, contentType);
+ string installPath = context.Device.FileSystem.SwitchPathToSystemPath(contentPath);
+
+ if (!string.IsNullOrWhiteSpace(installPath))
+ {
+ string ncaPath = installPath;
+
+ if (File.Exists(ncaPath))
+ {
+ try
+ {
+ LibHac.Fs.IStorage ncaStorage = new LocalStorage(ncaPath, FileAccess.Read, FileMode.Open);
+ Nca nca = new Nca(context.Device.System.KeySet, ncaStorage);
+ LibHac.Fs.IStorage romfsStorage = nca.OpenStorage(NcaSectionType.Data, context.Device.System.FsIntegrityCheckLevel);
+
+ MakeObject(context, new FileSystemProxy.IStorage(romfsStorage));
+ }
+ catch (HorizonResultException ex)
+ {
+ return (ResultCode)ex.ResultValue.Value;
+ }
+
+ return ResultCode.Success;
+ }
+ else
+ {
+ throw new FileNotFoundException($"No Nca found in Path `{ncaPath}`.");
+ }
+ }
+ else
+ {
+ throw new DirectoryNotFoundException($"Path for title id {titleId:x16} on Storage {storageId} was not found in Path {installPath}.");
+ }
+ }
+
+ throw new FileNotFoundException($"System archive with titleid {titleId:x16} was not found on Storage {storageId}. Found in {installedStorage}.");
+ }
+
+ [Command(203)]
+ // OpenPatchDataStorageByCurrentProcess() -> object<nn::fssrv::sf::IStorage>
+ public ResultCode OpenPatchDataStorageByCurrentProcess(ServiceCtx context)
+ {
+ MakeObject(context, new FileSystemProxy.IStorage(context.Device.FileSystem.RomFs.AsStorage()));
+
+ return ResultCode.Success;
+ }
+
+ [Command(1005)]
+ // GetGlobalAccessLogMode() -> u32 logMode
+ public ResultCode GetGlobalAccessLogMode(ServiceCtx context)
+ {
+ int mode = context.Device.System.GlobalAccessLogMode;
+
+ context.ResponseData.Write(mode);
+
+ return ResultCode.Success;
+ }
+
+ [Command(1006)]
+ // OutputAccessLogToSdCard(buffer<bytes, 5> log_text)
+ public ResultCode OutputAccessLogToSdCard(ServiceCtx context)
+ {
+ string message = ReadUtf8StringSend(context);
+
+ // FS ends each line with a newline. Remove it because Ryujinx logging adds its own newline
+ Logger.PrintAccessLog(LogClass.ServiceFs, message.TrimEnd('\n'));
+
+ return ResultCode.Success;
+ }
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.HLE/HOS/Services/Fs/IFileSystemProxyForLoader.cs b/Ryujinx.HLE/HOS/Services/Fs/IFileSystemProxyForLoader.cs
new file mode 100644
index 00000000..a40821b9
--- /dev/null
+++ b/Ryujinx.HLE/HOS/Services/Fs/IFileSystemProxyForLoader.cs
@@ -0,0 +1,8 @@
+namespace Ryujinx.HLE.HOS.Services.Fs
+{
+ [Service("fsp-ldr")]
+ class IFileSystemProxyForLoader : IpcService
+ {
+ public IFileSystemProxyForLoader(ServiceCtx context) { }
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.HLE/HOS/Services/Fs/IProgramRegistry.cs b/Ryujinx.HLE/HOS/Services/Fs/IProgramRegistry.cs
new file mode 100644
index 00000000..e11eadf5
--- /dev/null
+++ b/Ryujinx.HLE/HOS/Services/Fs/IProgramRegistry.cs
@@ -0,0 +1,8 @@
+namespace Ryujinx.HLE.HOS.Services.Fs
+{
+ [Service("fsp-pr")]
+ class IProgramRegistry : IpcService
+ {
+ public IProgramRegistry(ServiceCtx context) { }
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.HLE/HOS/Services/Fs/ResultCode.cs b/Ryujinx.HLE/HOS/Services/Fs/ResultCode.cs
new file mode 100644
index 00000000..8f87142b
--- /dev/null
+++ b/Ryujinx.HLE/HOS/Services/Fs/ResultCode.cs
@@ -0,0 +1,16 @@
+namespace Ryujinx.HLE.HOS.Services.Fs
+{
+ enum ResultCode
+ {
+ ModuleId = 2,
+ ErrorCodeShift = 9,
+
+ Success = 0,
+
+ PathDoesNotExist = (1 << ErrorCodeShift) | ModuleId,
+ PathAlreadyExists = (2 << ErrorCodeShift) | ModuleId,
+ PathAlreadyInUse = (7 << ErrorCodeShift) | ModuleId,
+ PartitionNotFound = (1001 << ErrorCodeShift) | ModuleId,
+ InvalidInput = (6001 << ErrorCodeShift) | ModuleId
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.HLE/HOS/Services/Fs/Types/FileSystemType.cs b/Ryujinx.HLE/HOS/Services/Fs/Types/FileSystemType.cs
new file mode 100644
index 00000000..f12c1661
--- /dev/null
+++ b/Ryujinx.HLE/HOS/Services/Fs/Types/FileSystemType.cs
@@ -0,0 +1,12 @@
+namespace Ryujinx.HLE.HOS.Services.Fs
+{
+ enum FileSystemType
+ {
+ Logo = 2,
+ ContentControl = 3,
+ ContentManual = 4,
+ ContentMeta = 5,
+ ContentData = 6,
+ ApplicationPackage = 7
+ }
+} \ No newline at end of file