diff options
Diffstat (limited to 'src/Ryujinx.HLE/FileSystem')
-rw-r--r-- | src/Ryujinx.HLE/FileSystem/ContentManager.cs | 1048 | ||||
-rw-r--r-- | src/Ryujinx.HLE/FileSystem/ContentPath.cs | 82 | ||||
-rw-r--r-- | src/Ryujinx.HLE/FileSystem/EncryptedFileSystemCreator.cs | 26 | ||||
-rw-r--r-- | src/Ryujinx.HLE/FileSystem/LocationEntry.cs | 25 | ||||
-rw-r--r-- | src/Ryujinx.HLE/FileSystem/SystemVersion.cs | 40 | ||||
-rw-r--r-- | src/Ryujinx.HLE/FileSystem/VirtualFileSystem.cs | 615 |
6 files changed, 1836 insertions, 0 deletions
diff --git a/src/Ryujinx.HLE/FileSystem/ContentManager.cs b/src/Ryujinx.HLE/FileSystem/ContentManager.cs new file mode 100644 index 00000000..9facdd0b --- /dev/null +++ b/src/Ryujinx.HLE/FileSystem/ContentManager.cs @@ -0,0 +1,1048 @@ +using LibHac.Common; +using LibHac.Common.Keys; +using LibHac.Fs; +using LibHac.Fs.Fsa; +using LibHac.FsSystem; +using LibHac.Ncm; +using LibHac.Tools.Fs; +using LibHac.Tools.FsSystem; +using LibHac.Tools.FsSystem.NcaUtils; +using LibHac.Tools.Ncm; +using Ryujinx.Common.Logging; +using Ryujinx.Common.Memory; +using Ryujinx.Common.Utilities; +using Ryujinx.HLE.Exceptions; +using Ryujinx.HLE.HOS.Services.Ssl; +using Ryujinx.HLE.HOS.Services.Time; +using System; +using System.Collections.Generic; +using System.IO; +using System.IO.Compression; +using System.Linq; +using Path = System.IO.Path; + +namespace Ryujinx.HLE.FileSystem +{ + public class ContentManager + { + private const ulong SystemVersionTitleId = 0x0100000000000809; + private const ulong SystemUpdateTitleId = 0x0100000000000816; + + private Dictionary<StorageId, LinkedList<LocationEntry>> _locationEntries; + + private Dictionary<string, ulong> _sharedFontTitleDictionary; + private Dictionary<ulong, string> _systemTitlesNameDictionary; + private Dictionary<string, string> _sharedFontFilenameDictionary; + + private SortedDictionary<(ulong titleId, NcaContentType type), string> _contentDictionary; + + private struct AocItem + { + public readonly string ContainerPath; + public readonly string NcaPath; + + public AocItem(string containerPath, string ncaPath) + { + ContainerPath = containerPath; + NcaPath = ncaPath; + } + } + + private SortedList<ulong, AocItem> _aocData { get; } + + private VirtualFileSystem _virtualFileSystem; + + private readonly object _lock = new(); + + public ContentManager(VirtualFileSystem virtualFileSystem) + { + _contentDictionary = new SortedDictionary<(ulong, NcaContentType), string>(); + _locationEntries = new Dictionary<StorageId, LinkedList<LocationEntry>>(); + + _sharedFontTitleDictionary = new Dictionary<string, ulong> + { + { "FontStandard", 0x0100000000000811 }, + { "FontChineseSimplified", 0x0100000000000814 }, + { "FontExtendedChineseSimplified", 0x0100000000000814 }, + { "FontKorean", 0x0100000000000812 }, + { "FontChineseTraditional", 0x0100000000000813 }, + { "FontNintendoExtended", 0x0100000000000810 } + }; + + _systemTitlesNameDictionary = new Dictionary<ulong, string>() + { + { 0x010000000000080E, "TimeZoneBinary" }, + { 0x0100000000000810, "FontNintendoExtension" }, + { 0x0100000000000811, "FontStandard" }, + { 0x0100000000000812, "FontKorean" }, + { 0x0100000000000813, "FontChineseTraditional" }, + { 0x0100000000000814, "FontChineseSimple" }, + }; + + _sharedFontFilenameDictionary = new Dictionary<string, string> + { + { "FontStandard", "nintendo_udsg-r_std_003.bfttf" }, + { "FontChineseSimplified", "nintendo_udsg-r_org_zh-cn_003.bfttf" }, + { "FontExtendedChineseSimplified", "nintendo_udsg-r_ext_zh-cn_003.bfttf" }, + { "FontKorean", "nintendo_udsg-r_ko_003.bfttf" }, + { "FontChineseTraditional", "nintendo_udjxh-db_zh-tw_003.bfttf" }, + { "FontNintendoExtended", "nintendo_ext_003.bfttf" } + }; + + _virtualFileSystem = virtualFileSystem; + + _aocData = new SortedList<ulong, AocItem>(); + } + + public void LoadEntries(Switch device = null) + { + lock (_lock) + { + _contentDictionary = new SortedDictionary<(ulong, NcaContentType), string>(); + _locationEntries = new Dictionary<StorageId, LinkedList<LocationEntry>>(); + + foreach (StorageId storageId in Enum.GetValues<StorageId>()) + { + string contentDirectory = null; + string contentPathString = null; + string registeredDirectory = null; + + try + { + contentPathString = ContentPath.GetContentPath(storageId); + contentDirectory = ContentPath.GetRealPath(_virtualFileSystem, contentPathString); + registeredDirectory = Path.Combine(contentDirectory, "registered"); + } + catch (NotSupportedException) + { + continue; + } + + Directory.CreateDirectory(registeredDirectory); + + LinkedList<LocationEntry> locationList = new LinkedList<LocationEntry>(); + + void AddEntry(LocationEntry entry) + { + locationList.AddLast(entry); + } + + foreach (string directoryPath in Directory.EnumerateDirectories(registeredDirectory)) + { + if (Directory.GetFiles(directoryPath).Length > 0) + { + string ncaName = new DirectoryInfo(directoryPath).Name.Replace(".nca", string.Empty); + + using (FileStream ncaFile = File.OpenRead(Directory.GetFiles(directoryPath)[0])) + { + Nca nca = new Nca(_virtualFileSystem.KeySet, ncaFile.AsStorage()); + + string switchPath = contentPathString + ":/" + ncaFile.Name.Replace(contentDirectory, string.Empty).TrimStart(Path.DirectorySeparatorChar); + + // Change path format to switch's + switchPath = switchPath.Replace('\\', '/'); + + LocationEntry entry = new LocationEntry(switchPath, + 0, + nca.Header.TitleId, + nca.Header.ContentType); + + AddEntry(entry); + + _contentDictionary.Add((nca.Header.TitleId, nca.Header.ContentType), ncaName); + } + } + } + + foreach (string filePath in Directory.EnumerateFiles(contentDirectory)) + { + if (Path.GetExtension(filePath) == ".nca") + { + string ncaName = Path.GetFileNameWithoutExtension(filePath); + + using (FileStream ncaFile = new FileStream(filePath, FileMode.Open, FileAccess.Read)) + { + Nca nca = new Nca(_virtualFileSystem.KeySet, ncaFile.AsStorage()); + + string switchPath = contentPathString + ":/" + filePath.Replace(contentDirectory, string.Empty).TrimStart(Path.DirectorySeparatorChar); + + // Change path format to switch's + switchPath = switchPath.Replace('\\', '/'); + + LocationEntry entry = new LocationEntry(switchPath, + 0, + nca.Header.TitleId, + nca.Header.ContentType); + + AddEntry(entry); + + _contentDictionary.Add((nca.Header.TitleId, nca.Header.ContentType), ncaName); + } + } + } + + if (_locationEntries.ContainsKey(storageId) && _locationEntries[storageId]?.Count == 0) + { + _locationEntries.Remove(storageId); + } + + if (!_locationEntries.ContainsKey(storageId)) + { + _locationEntries.Add(storageId, locationList); + } + } + + if (device != null) + { + TimeManager.Instance.InitializeTimeZone(device); + BuiltInCertificateManager.Instance.Initialize(device); + device.System.SharedFontManager.Initialize(); + } + } + } + + // fs must contain AOC nca files in its root + public void AddAocData(IFileSystem fs, string containerPath, ulong aocBaseId, IntegrityCheckLevel integrityCheckLevel) + { + _virtualFileSystem.ImportTickets(fs); + + foreach (var ncaPath in fs.EnumerateEntries("*.cnmt.nca", SearchOptions.Default)) + { + using var ncaFile = new UniqueRef<IFile>(); + + fs.OpenFile(ref ncaFile.Ref, ncaPath.FullPath.ToU8Span(), OpenMode.Read); + var nca = new Nca(_virtualFileSystem.KeySet, ncaFile.Get.AsStorage()); + if (nca.Header.ContentType != NcaContentType.Meta) + { + Logger.Warning?.Print(LogClass.Application, $"{ncaPath} is not a valid metadata file"); + + continue; + } + + using var pfs0 = nca.OpenFileSystem(0, integrityCheckLevel); + using var cnmtFile = new UniqueRef<IFile>(); + + pfs0.OpenFile(ref cnmtFile.Ref, pfs0.EnumerateEntries().Single().FullPath.ToU8Span(), OpenMode.Read); + + var cnmt = new Cnmt(cnmtFile.Get.AsStream()); + if (cnmt.Type != ContentMetaType.AddOnContent || (cnmt.TitleId & 0xFFFFFFFFFFFFE000) != aocBaseId) + { + continue; + } + + string ncaId = Convert.ToHexString(cnmt.ContentEntries[0].NcaId).ToLower(); + + AddAocItem(cnmt.TitleId, containerPath, $"{ncaId}.nca", true); + } + } + + public void AddAocItem(ulong titleId, string containerPath, string ncaPath, bool mergedToContainer = false) + { + // TODO: Check Aoc version. + if (!_aocData.TryAdd(titleId, new AocItem(containerPath, ncaPath))) + { + Logger.Warning?.Print(LogClass.Application, $"Duplicate AddOnContent detected. TitleId {titleId:X16}"); + } + else + { + Logger.Info?.Print(LogClass.Application, $"Found AddOnContent with TitleId {titleId:X16}"); + + if (!mergedToContainer) + { + using FileStream fileStream = File.OpenRead(containerPath); + using PartitionFileSystem partitionFileSystem = new(fileStream.AsStorage()); + + _virtualFileSystem.ImportTickets(partitionFileSystem); + } + } + } + + public void ClearAocData() => _aocData.Clear(); + + public int GetAocCount() => _aocData.Count; + + public IList<ulong> GetAocTitleIds() => _aocData.Select(e => e.Key).ToList(); + + public bool GetAocDataStorage(ulong aocTitleId, out IStorage aocStorage, IntegrityCheckLevel integrityCheckLevel) + { + aocStorage = null; + + if (_aocData.TryGetValue(aocTitleId, out AocItem aoc)) + { + var file = new FileStream(aoc.ContainerPath, FileMode.Open, FileAccess.Read); + using var ncaFile = new UniqueRef<IFile>(); + PartitionFileSystem pfs; + + switch (Path.GetExtension(aoc.ContainerPath)) + { + case ".xci": + pfs = new Xci(_virtualFileSystem.KeySet, file.AsStorage()).OpenPartition(XciPartitionType.Secure); + pfs.OpenFile(ref ncaFile.Ref, aoc.NcaPath.ToU8Span(), OpenMode.Read); + break; + case ".nsp": + pfs = new PartitionFileSystem(file.AsStorage()); + pfs.OpenFile(ref ncaFile.Ref, aoc.NcaPath.ToU8Span(), OpenMode.Read); + break; + default: + return false; // Print error? + } + + aocStorage = new Nca(_virtualFileSystem.KeySet, ncaFile.Get.AsStorage()).OpenStorage(NcaSectionType.Data, integrityCheckLevel); + + return true; + } + + return false; + } + + public void ClearEntry(ulong titleId, NcaContentType contentType, StorageId storageId) + { + lock (_lock) + { + RemoveLocationEntry(titleId, contentType, storageId); + } + } + + public void RefreshEntries(StorageId storageId, int flag) + { + lock (_lock) + { + LinkedList<LocationEntry> locationList = _locationEntries[storageId]; + LinkedListNode<LocationEntry> locationEntry = locationList.First; + + while (locationEntry != null) + { + LinkedListNode<LocationEntry> nextLocationEntry = locationEntry.Next; + + if (locationEntry.Value.Flag == flag) + { + locationList.Remove(locationEntry.Value); + } + + locationEntry = nextLocationEntry; + } + } + } + + public bool HasNca(string ncaId, StorageId storageId) + { + lock (_lock) + { + if (_contentDictionary.ContainsValue(ncaId)) + { + var content = _contentDictionary.FirstOrDefault(x => x.Value == ncaId); + ulong titleId = content.Key.Item1; + + NcaContentType contentType = content.Key.type; + StorageId storage = GetInstalledStorage(titleId, contentType, storageId); + + return storage == storageId; + } + } + + return false; + } + + public UInt128 GetInstalledNcaId(ulong titleId, NcaContentType contentType) + { + lock (_lock) + { + if (_contentDictionary.ContainsKey((titleId, contentType))) + { + return UInt128Utils.FromHex(_contentDictionary[(titleId, contentType)]); + } + } + + return new UInt128(); + } + + public StorageId GetInstalledStorage(ulong titleId, NcaContentType contentType, StorageId storageId) + { + lock (_lock) + { + LocationEntry locationEntry = GetLocation(titleId, contentType, storageId); + + return locationEntry.ContentPath != null ? ContentPath.GetStorageId(locationEntry.ContentPath) : StorageId.None; + } + } + + public string GetInstalledContentPath(ulong titleId, StorageId storageId, NcaContentType contentType) + { + lock (_lock) + { + LocationEntry locationEntry = GetLocation(titleId, contentType, storageId); + + if (VerifyContentType(locationEntry, contentType)) + { + return locationEntry.ContentPath; + } + } + + return string.Empty; + } + + public void RedirectLocation(LocationEntry newEntry, StorageId storageId) + { + lock (_lock) + { + LocationEntry locationEntry = GetLocation(newEntry.TitleId, newEntry.ContentType, storageId); + + if (locationEntry.ContentPath != null) + { + RemoveLocationEntry(newEntry.TitleId, newEntry.ContentType, storageId); + } + + AddLocationEntry(newEntry, storageId); + } + } + + private bool VerifyContentType(LocationEntry locationEntry, NcaContentType contentType) + { + if (locationEntry.ContentPath == null) + { + return false; + } + + string installedPath = _virtualFileSystem.SwitchPathToSystemPath(locationEntry.ContentPath); + + if (!string.IsNullOrWhiteSpace(installedPath)) + { + if (File.Exists(installedPath)) + { + using (FileStream file = new FileStream(installedPath, FileMode.Open, FileAccess.Read)) + { + Nca nca = new Nca(_virtualFileSystem.KeySet, file.AsStorage()); + bool contentCheck = nca.Header.ContentType == contentType; + + return contentCheck; + } + } + } + + return false; + } + + private void AddLocationEntry(LocationEntry entry, StorageId storageId) + { + LinkedList<LocationEntry> locationList = null; + + if (_locationEntries.ContainsKey(storageId)) + { + locationList = _locationEntries[storageId]; + } + + if (locationList != null) + { + if (locationList.Contains(entry)) + { + locationList.Remove(entry); + } + + locationList.AddLast(entry); + } + } + + private void RemoveLocationEntry(ulong titleId, NcaContentType contentType, StorageId storageId) + { + LinkedList<LocationEntry> locationList = null; + + if (_locationEntries.ContainsKey(storageId)) + { + locationList = _locationEntries[storageId]; + } + + if (locationList != null) + { + LocationEntry entry = + locationList.ToList().Find(x => x.TitleId == titleId && x.ContentType == contentType); + + if (entry.ContentPath != null) + { + locationList.Remove(entry); + } + } + } + + public bool TryGetFontTitle(string fontName, out ulong titleId) + { + return _sharedFontTitleDictionary.TryGetValue(fontName, out titleId); + } + + public bool TryGetFontFilename(string fontName, out string filename) + { + return _sharedFontFilenameDictionary.TryGetValue(fontName, out filename); + } + + public bool TryGetSystemTitlesName(ulong titleId, out string name) + { + return _systemTitlesNameDictionary.TryGetValue(titleId, out name); + } + + private LocationEntry GetLocation(ulong titleId, NcaContentType contentType, StorageId storageId) + { + LinkedList<LocationEntry> locationList = _locationEntries[storageId]; + + return locationList.ToList().Find(x => x.TitleId == titleId && x.ContentType == contentType); + } + + public void InstallFirmware(string firmwareSource) + { + string contentPathString = ContentPath.GetContentPath(StorageId.BuiltInSystem); + string contentDirectory = ContentPath.GetRealPath(_virtualFileSystem, contentPathString); + string registeredDirectory = Path.Combine(contentDirectory, "registered"); + string temporaryDirectory = Path.Combine(contentDirectory, "temp"); + + if (Directory.Exists(temporaryDirectory)) + { + Directory.Delete(temporaryDirectory, true); + } + + if (Directory.Exists(firmwareSource)) + { + InstallFromDirectory(firmwareSource, temporaryDirectory); + FinishInstallation(temporaryDirectory, registeredDirectory); + + return; + } + + if (!File.Exists(firmwareSource)) + { + throw new FileNotFoundException("Firmware file does not exist."); + } + + FileInfo info = new FileInfo(firmwareSource); + + using (FileStream file = File.OpenRead(firmwareSource)) + { + switch (info.Extension) + { + case ".zip": + using (ZipArchive archive = ZipFile.OpenRead(firmwareSource)) + { + InstallFromZip(archive, temporaryDirectory); + } + break; + case ".xci": + Xci xci = new Xci(_virtualFileSystem.KeySet, file.AsStorage()); + InstallFromCart(xci, temporaryDirectory); + break; + default: + throw new InvalidFirmwarePackageException("Input file is not a valid firmware package"); + } + + FinishInstallation(temporaryDirectory, registeredDirectory); + } + } + + private void FinishInstallation(string temporaryDirectory, string registeredDirectory) + { + if (Directory.Exists(registeredDirectory)) + { + new DirectoryInfo(registeredDirectory).Delete(true); + } + + Directory.Move(temporaryDirectory, registeredDirectory); + + LoadEntries(); + } + + private void InstallFromDirectory(string firmwareDirectory, string temporaryDirectory) + { + InstallFromPartition(new LocalFileSystem(firmwareDirectory), temporaryDirectory); + } + + private void InstallFromPartition(IFileSystem filesystem, string temporaryDirectory) + { + foreach (var entry in filesystem.EnumerateEntries("/", "*.nca")) + { + Nca nca = new Nca(_virtualFileSystem.KeySet, OpenPossibleFragmentedFile(filesystem, entry.FullPath, OpenMode.Read).AsStorage()); + + SaveNca(nca, entry.Name.Remove(entry.Name.IndexOf('.')), temporaryDirectory); + } + } + + private void InstallFromCart(Xci gameCard, string temporaryDirectory) + { + if (gameCard.HasPartition(XciPartitionType.Update)) + { + XciPartition partition = gameCard.OpenPartition(XciPartitionType.Update); + + InstallFromPartition(partition, temporaryDirectory); + } + else + { + throw new Exception("Update not found in xci file."); + } + } + + private void InstallFromZip(ZipArchive archive, string temporaryDirectory) + { + using (archive) + { + foreach (var entry in archive.Entries) + { + if (entry.FullName.EndsWith(".nca") || entry.FullName.EndsWith(".nca/00")) + { + // Clean up the name and get the NcaId + + string[] pathComponents = entry.FullName.Replace(".cnmt", "").Split('/'); + + string ncaId = pathComponents[pathComponents.Length - 1]; + + // If this is a fragmented nca, we need to get the previous element.GetZip + if (ncaId.Equals("00")) + { + ncaId = pathComponents[pathComponents.Length - 2]; + } + + if (ncaId.Contains(".nca")) + { + string newPath = Path.Combine(temporaryDirectory, ncaId); + + Directory.CreateDirectory(newPath); + + entry.ExtractToFile(Path.Combine(newPath, "00")); + } + } + } + } + } + + public void SaveNca(Nca nca, string ncaId, string temporaryDirectory) + { + string newPath = Path.Combine(temporaryDirectory, ncaId + ".nca"); + + Directory.CreateDirectory(newPath); + + using (FileStream file = File.Create(Path.Combine(newPath, "00"))) + { + nca.BaseStorage.AsStream().CopyTo(file); + } + } + + private IFile OpenPossibleFragmentedFile(IFileSystem filesystem, string path, OpenMode mode) + { + using var file = new UniqueRef<IFile>(); + + if (filesystem.FileExists($"{path}/00")) + { + filesystem.OpenFile(ref file.Ref, $"{path}/00".ToU8Span(), mode); + } + else + { + filesystem.OpenFile(ref file.Ref, path.ToU8Span(), mode); + } + + return file.Release(); + } + + private Stream GetZipStream(ZipArchiveEntry entry) + { + MemoryStream dest = MemoryStreamManager.Shared.GetStream(); + + using (Stream src = entry.Open()) + { + src.CopyTo(dest); + } + + return dest; + } + + public SystemVersion VerifyFirmwarePackage(string firmwarePackage) + { + _virtualFileSystem.ReloadKeySet(); + + // LibHac.NcaHeader's DecryptHeader doesn't check if HeaderKey is empty and throws InvalidDataException instead + // So, we check it early for a better user experience. + if (_virtualFileSystem.KeySet.HeaderKey.IsZeros()) + { + throw new MissingKeyException("HeaderKey is empty. Cannot decrypt NCA headers."); + } + + Dictionary<ulong, List<(NcaContentType type, string path)>> updateNcas = new Dictionary<ulong, List<(NcaContentType, string)>>(); + + if (Directory.Exists(firmwarePackage)) + { + return VerifyAndGetVersionDirectory(firmwarePackage); + } + + if (!File.Exists(firmwarePackage)) + { + throw new FileNotFoundException("Firmware file does not exist."); + } + + FileInfo info = new FileInfo(firmwarePackage); + + using (FileStream file = File.OpenRead(firmwarePackage)) + { + switch (info.Extension) + { + case ".zip": + using (ZipArchive archive = ZipFile.OpenRead(firmwarePackage)) + { + return VerifyAndGetVersionZip(archive); + } + case ".xci": + Xci xci = new Xci(_virtualFileSystem.KeySet, file.AsStorage()); + + if (xci.HasPartition(XciPartitionType.Update)) + { + XciPartition partition = xci.OpenPartition(XciPartitionType.Update); + + return VerifyAndGetVersion(partition); + } + else + { + throw new InvalidFirmwarePackageException("Update not found in xci file."); + } + default: + break; + } + } + + SystemVersion VerifyAndGetVersionDirectory(string firmwareDirectory) + { + return VerifyAndGetVersion(new LocalFileSystem(firmwareDirectory)); + } + + SystemVersion VerifyAndGetVersionZip(ZipArchive archive) + { + SystemVersion systemVersion = null; + + foreach (var entry in archive.Entries) + { + if (entry.FullName.EndsWith(".nca") || entry.FullName.EndsWith(".nca/00")) + { + using (Stream ncaStream = GetZipStream(entry)) + { + IStorage storage = ncaStream.AsStorage(); + + Nca nca = new Nca(_virtualFileSystem.KeySet, storage); + + if (updateNcas.ContainsKey(nca.Header.TitleId)) + { + updateNcas[nca.Header.TitleId].Add((nca.Header.ContentType, entry.FullName)); + } + else + { + updateNcas.Add(nca.Header.TitleId, new List<(NcaContentType, string)>()); + updateNcas[nca.Header.TitleId].Add((nca.Header.ContentType, entry.FullName)); + } + } + } + } + + if (updateNcas.ContainsKey(SystemUpdateTitleId)) + { + var ncaEntry = updateNcas[SystemUpdateTitleId]; + + string metaPath = ncaEntry.Find(x => x.type == NcaContentType.Meta).path; + + CnmtContentMetaEntry[] metaEntries = null; + + var fileEntry = archive.GetEntry(metaPath); + + using (Stream ncaStream = GetZipStream(fileEntry)) + { + Nca metaNca = new Nca(_virtualFileSystem.KeySet, ncaStream.AsStorage()); + + IFileSystem fs = metaNca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid); + + string cnmtPath = fs.EnumerateEntries("/", "*.cnmt").Single().FullPath; + + using var metaFile = new UniqueRef<IFile>(); + + if (fs.OpenFile(ref metaFile.Ref, cnmtPath.ToU8Span(), OpenMode.Read).IsSuccess()) + { + var meta = new Cnmt(metaFile.Get.AsStream()); + + if (meta.Type == ContentMetaType.SystemUpdate) + { + metaEntries = meta.MetaEntries; + + updateNcas.Remove(SystemUpdateTitleId); + } + } + } + + if (metaEntries == null) + { + throw new FileNotFoundException("System update title was not found in the firmware package."); + } + + if (updateNcas.ContainsKey(SystemVersionTitleId)) + { + string versionEntry = updateNcas[SystemVersionTitleId].Find(x => x.type != NcaContentType.Meta).path; + + using (Stream ncaStream = GetZipStream(archive.GetEntry(versionEntry))) + { + Nca nca = new Nca(_virtualFileSystem.KeySet, ncaStream.AsStorage()); + + var romfs = nca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid); + + using var systemVersionFile = new UniqueRef<IFile>(); + + if (romfs.OpenFile(ref systemVersionFile.Ref, "/file".ToU8Span(), OpenMode.Read).IsSuccess()) + { + systemVersion = new SystemVersion(systemVersionFile.Get.AsStream()); + } + } + } + + foreach (CnmtContentMetaEntry metaEntry in metaEntries) + { + if (updateNcas.TryGetValue(metaEntry.TitleId, out ncaEntry)) + { + metaPath = ncaEntry.Find(x => x.type == NcaContentType.Meta).path; + + string contentPath = ncaEntry.Find(x => x.type != NcaContentType.Meta).path; + + // Nintendo in 9.0.0, removed PPC and only kept the meta nca of it. + // This is a perfect valid case, so we should just ignore the missing content nca and continue. + if (contentPath == null) + { + updateNcas.Remove(metaEntry.TitleId); + + continue; + } + + ZipArchiveEntry metaZipEntry = archive.GetEntry(metaPath); + ZipArchiveEntry contentZipEntry = archive.GetEntry(contentPath); + + using (Stream metaNcaStream = GetZipStream(metaZipEntry)) + { + using (Stream contentNcaStream = GetZipStream(contentZipEntry)) + { + Nca metaNca = new Nca(_virtualFileSystem.KeySet, metaNcaStream.AsStorage()); + + IFileSystem fs = metaNca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid); + + string cnmtPath = fs.EnumerateEntries("/", "*.cnmt").Single().FullPath; + + using var metaFile = new UniqueRef<IFile>(); + + if (fs.OpenFile(ref metaFile.Ref, cnmtPath.ToU8Span(), OpenMode.Read).IsSuccess()) + { + var meta = new Cnmt(metaFile.Get.AsStream()); + + IStorage contentStorage = contentNcaStream.AsStorage(); + if (contentStorage.GetSize(out long size).IsSuccess()) + { + byte[] contentData = new byte[size]; + + Span<byte> content = new Span<byte>(contentData); + + contentStorage.Read(0, content); + + Span<byte> hash = new Span<byte>(new byte[32]); + + LibHac.Crypto.Sha256.GenerateSha256Hash(content, hash); + + if (LibHac.Common.Utilities.ArraysEqual(hash.ToArray(), meta.ContentEntries[0].Hash)) + { + updateNcas.Remove(metaEntry.TitleId); + } + } + } + } + } + } + } + + if (updateNcas.Count > 0) + { + string extraNcas = string.Empty; + + foreach (var entry in updateNcas) + { + foreach (var nca in entry.Value) + { + extraNcas += nca.path + Environment.NewLine; + } + } + + throw new InvalidFirmwarePackageException($"Firmware package contains unrelated archives. Please remove these paths: {Environment.NewLine}{extraNcas}"); + } + } + else + { + throw new FileNotFoundException("System update title was not found in the firmware package."); + } + + return systemVersion; + } + + SystemVersion VerifyAndGetVersion(IFileSystem filesystem) + { + SystemVersion systemVersion = null; + + CnmtContentMetaEntry[] metaEntries = null; + + foreach (var entry in filesystem.EnumerateEntries("/", "*.nca")) + { + IStorage ncaStorage = OpenPossibleFragmentedFile(filesystem, entry.FullPath, OpenMode.Read).AsStorage(); + + Nca nca = new Nca(_virtualFileSystem.KeySet, ncaStorage); + + if (nca.Header.TitleId == SystemUpdateTitleId && nca.Header.ContentType == NcaContentType.Meta) + { + IFileSystem fs = nca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid); + + string cnmtPath = fs.EnumerateEntries("/", "*.cnmt").Single().FullPath; + + using var metaFile = new UniqueRef<IFile>(); + + if (fs.OpenFile(ref metaFile.Ref, cnmtPath.ToU8Span(), OpenMode.Read).IsSuccess()) + { + var meta = new Cnmt(metaFile.Get.AsStream()); + + if (meta.Type == ContentMetaType.SystemUpdate) + { + metaEntries = meta.MetaEntries; + } + } + + continue; + } + else if (nca.Header.TitleId == SystemVersionTitleId && nca.Header.ContentType == NcaContentType.Data) + { + var romfs = nca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid); + + using var systemVersionFile = new UniqueRef<IFile>(); + + if (romfs.OpenFile(ref systemVersionFile.Ref, "/file".ToU8Span(), OpenMode.Read).IsSuccess()) + { + systemVersion = new SystemVersion(systemVersionFile.Get.AsStream()); + } + } + + if (updateNcas.ContainsKey(nca.Header.TitleId)) + { + updateNcas[nca.Header.TitleId].Add((nca.Header.ContentType, entry.FullPath)); + } + else + { + updateNcas.Add(nca.Header.TitleId, new List<(NcaContentType, string)>()); + updateNcas[nca.Header.TitleId].Add((nca.Header.ContentType, entry.FullPath)); + } + + ncaStorage.Dispose(); + } + + if (metaEntries == null) + { + throw new FileNotFoundException("System update title was not found in the firmware package."); + } + + foreach (CnmtContentMetaEntry metaEntry in metaEntries) + { + if (updateNcas.TryGetValue(metaEntry.TitleId, out var ncaEntry)) + { + var metaNcaEntry = ncaEntry.Find(x => x.type == NcaContentType.Meta); + string contentPath = ncaEntry.Find(x => x.type != NcaContentType.Meta).path; + + // Nintendo in 9.0.0, removed PPC and only kept the meta nca of it. + // This is a perfect valid case, so we should just ignore the missing content nca and continue. + if (contentPath == null) + { + updateNcas.Remove(metaEntry.TitleId); + + continue; + } + + IStorage metaStorage = OpenPossibleFragmentedFile(filesystem, metaNcaEntry.path, OpenMode.Read).AsStorage(); + IStorage contentStorage = OpenPossibleFragmentedFile(filesystem, contentPath, OpenMode.Read).AsStorage(); + + Nca metaNca = new Nca(_virtualFileSystem.KeySet, metaStorage); + + IFileSystem fs = metaNca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid); + + string cnmtPath = fs.EnumerateEntries("/", "*.cnmt").Single().FullPath; + + using var metaFile = new UniqueRef<IFile>(); + + if (fs.OpenFile(ref metaFile.Ref, cnmtPath.ToU8Span(), OpenMode.Read).IsSuccess()) + { + var meta = new Cnmt(metaFile.Get.AsStream()); + + if (contentStorage.GetSize(out long size).IsSuccess()) + { + byte[] contentData = new byte[size]; + + Span<byte> content = new Span<byte>(contentData); + + contentStorage.Read(0, content); + + Span<byte> hash = new Span<byte>(new byte[32]); + + LibHac.Crypto.Sha256.GenerateSha256Hash(content, hash); + + if (LibHac.Common.Utilities.ArraysEqual(hash.ToArray(), meta.ContentEntries[0].Hash)) + { + updateNcas.Remove(metaEntry.TitleId); + } + } + } + } + } + + if (updateNcas.Count > 0) + { + string extraNcas = string.Empty; + + foreach (var entry in updateNcas) + { + foreach (var (type, path) in entry.Value) + { + extraNcas += path + Environment.NewLine; + } + } + + throw new InvalidFirmwarePackageException($"Firmware package contains unrelated archives. Please remove these paths: {Environment.NewLine}{extraNcas}"); + } + + return systemVersion; + } + + return null; + } + + public SystemVersion GetCurrentFirmwareVersion() + { + LoadEntries(); + + lock (_lock) + { + var locationEnties = _locationEntries[StorageId.BuiltInSystem]; + + foreach (var entry in locationEnties) + { + if (entry.ContentType == NcaContentType.Data) + { + var path = _virtualFileSystem.SwitchPathToSystemPath(entry.ContentPath); + + using (FileStream fileStream = File.OpenRead(path)) + { + Nca nca = new Nca(_virtualFileSystem.KeySet, fileStream.AsStorage()); + + if (nca.Header.TitleId == SystemVersionTitleId && nca.Header.ContentType == NcaContentType.Data) + { + var romfs = nca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid); + + using var systemVersionFile = new UniqueRef<IFile>(); + + if (romfs.OpenFile(ref systemVersionFile.Ref, "/file".ToU8Span(), OpenMode.Read).IsSuccess()) + { + return new SystemVersion(systemVersionFile.Get.AsStream()); + } + } + + } + } + } + } + + return null; + } + } +} diff --git a/src/Ryujinx.HLE/FileSystem/ContentPath.cs b/src/Ryujinx.HLE/FileSystem/ContentPath.cs new file mode 100644 index 00000000..c8663081 --- /dev/null +++ b/src/Ryujinx.HLE/FileSystem/ContentPath.cs @@ -0,0 +1,82 @@ +using LibHac.Fs; +using LibHac.Ncm; +using Ryujinx.Common.Configuration; +using System; + +using static Ryujinx.HLE.FileSystem.VirtualFileSystem; +using Path = System.IO.Path; + +namespace Ryujinx.HLE.FileSystem +{ + internal static class ContentPath + { + public const string SystemContent = "@SystemContent"; + public const string UserContent = "@UserContent"; + public const string SdCardContent = "@SdCardContent"; + public const string SdCard = "@Sdcard"; + public const string CalibFile = "@CalibFile"; + public const string Safe = "@Safe"; + public const string User = "@User"; + public const string System = "@System"; + public const string Host = "@Host"; + public const string GamecardApp = "@GcApp"; + public const string GamecardContents = "@GcS00000001"; + public const string GamecardUpdate = "@upp"; + public const string RegisteredUpdate = "@RegUpdate"; + + public const string Nintendo = "Nintendo"; + public const string Contents = "Contents"; + + public static string GetRealPath(VirtualFileSystem fileSystem, string switchContentPath) + { + return switchContentPath switch + { + SystemContent => Path.Combine(AppDataManager.BaseDirPath, SystemNandPath, Contents), + UserContent => Path.Combine(AppDataManager.BaseDirPath, UserNandPath, Contents), + SdCardContent => Path.Combine(fileSystem.GetSdCardPath(), Nintendo, Contents), + System => Path.Combine(AppDataManager.BaseDirPath, SystemNandPath), + User => Path.Combine(AppDataManager.BaseDirPath, UserNandPath), + _ => throw new NotSupportedException($"Content Path \"`{switchContentPath}`\" is not supported.") + }; + } + + public static string GetContentPath(ContentStorageId contentStorageId) + { + return contentStorageId switch + { + ContentStorageId.System => SystemContent, + ContentStorageId.User => UserContent, + ContentStorageId.SdCard => SdCardContent, + _ => throw new NotSupportedException($"Content Storage Id \"`{contentStorageId}`\" is not supported.") + }; + } + + public static string GetContentPath(StorageId storageId) + { + return storageId switch + { + StorageId.BuiltInSystem => SystemContent, + StorageId.BuiltInUser => UserContent, + StorageId.SdCard => SdCardContent, + _ => throw new NotSupportedException($"Storage Id \"`{storageId}`\" is not supported.") + }; + } + + public static StorageId GetStorageId(string contentPathString) + { + return contentPathString.Split(':')[0] switch + { + SystemContent or + System => StorageId.BuiltInSystem, + UserContent or + User => StorageId.BuiltInUser, + SdCardContent => StorageId.SdCard, + Host => StorageId.Host, + GamecardApp or + GamecardContents or + GamecardUpdate => StorageId.GameCard, + _ => StorageId.None + }; + } + } +}
\ No newline at end of file diff --git a/src/Ryujinx.HLE/FileSystem/EncryptedFileSystemCreator.cs b/src/Ryujinx.HLE/FileSystem/EncryptedFileSystemCreator.cs new file mode 100644 index 00000000..f32dc2d7 --- /dev/null +++ b/src/Ryujinx.HLE/FileSystem/EncryptedFileSystemCreator.cs @@ -0,0 +1,26 @@ +using LibHac; +using LibHac.Common; +using LibHac.Fs; +using LibHac.Fs.Fsa; +using LibHac.FsSrv.FsCreator; + +namespace Ryujinx.HLE.FileSystem +{ + public class EncryptedFileSystemCreator : IEncryptedFileSystemCreator + { + public Result Create(ref SharedRef<IFileSystem> outEncryptedFileSystem, + ref SharedRef<IFileSystem> baseFileSystem, IEncryptedFileSystemCreator.KeyId idIndex, + in EncryptionSeed encryptionSeed) + { + if (idIndex < IEncryptedFileSystemCreator.KeyId.Save || idIndex > IEncryptedFileSystemCreator.KeyId.CustomStorage) + { + return ResultFs.InvalidArgument.Log(); + } + + // TODO: Reenable when AesXtsFileSystem is fixed. + outEncryptedFileSystem = SharedRef<IFileSystem>.CreateMove(ref baseFileSystem); + + return Result.Success; + } + } +}
\ No newline at end of file diff --git a/src/Ryujinx.HLE/FileSystem/LocationEntry.cs b/src/Ryujinx.HLE/FileSystem/LocationEntry.cs new file mode 100644 index 00000000..a60c2896 --- /dev/null +++ b/src/Ryujinx.HLE/FileSystem/LocationEntry.cs @@ -0,0 +1,25 @@ +using LibHac.Tools.FsSystem.NcaUtils; + +namespace Ryujinx.HLE.FileSystem +{ + public struct LocationEntry + { + public string ContentPath { get; private set; } + public int Flag { get; private set; } + public ulong TitleId { get; private set; } + public NcaContentType ContentType { get; private set; } + + public LocationEntry(string contentPath, int flag, ulong titleId, NcaContentType contentType) + { + ContentPath = contentPath; + Flag = flag; + TitleId = titleId; + ContentType = contentType; + } + + public void SetFlag(int flag) + { + Flag = flag; + } + } +} diff --git a/src/Ryujinx.HLE/FileSystem/SystemVersion.cs b/src/Ryujinx.HLE/FileSystem/SystemVersion.cs new file mode 100644 index 00000000..a7926d5d --- /dev/null +++ b/src/Ryujinx.HLE/FileSystem/SystemVersion.cs @@ -0,0 +1,40 @@ +using Ryujinx.HLE.Utilities; +using System.IO; + +namespace Ryujinx.HLE.FileSystem +{ + public class SystemVersion + { + public byte Major { get; } + public byte Minor { get; } + public byte Micro { get; } + public byte RevisionMajor { get; } + public byte RevisionMinor { get; } + public string PlatformString { get; } + public string Hex { get; } + public string VersionString { get; } + public string VersionTitle { get; } + + public SystemVersion(Stream systemVersionFile) + { + using (BinaryReader reader = new BinaryReader(systemVersionFile)) + { + Major = reader.ReadByte(); + Minor = reader.ReadByte(); + Micro = reader.ReadByte(); + + reader.ReadByte(); // Padding + + RevisionMajor = reader.ReadByte(); + RevisionMinor = reader.ReadByte(); + + reader.ReadBytes(2); // Padding + + PlatformString = StringUtils.ReadInlinedAsciiString(reader, 0x20); + Hex = StringUtils.ReadInlinedAsciiString(reader, 0x40); + VersionString = StringUtils.ReadInlinedAsciiString(reader, 0x18); + VersionTitle = StringUtils.ReadInlinedAsciiString(reader, 0x80); + } + } + } +}
\ No newline at end of file diff --git a/src/Ryujinx.HLE/FileSystem/VirtualFileSystem.cs b/src/Ryujinx.HLE/FileSystem/VirtualFileSystem.cs new file mode 100644 index 00000000..1b3968ea --- /dev/null +++ b/src/Ryujinx.HLE/FileSystem/VirtualFileSystem.cs @@ -0,0 +1,615 @@ +using LibHac; +using LibHac.Common; +using LibHac.Common.Keys; +using LibHac.Fs; +using LibHac.Fs.Fsa; +using LibHac.Fs.Shim; +using LibHac.FsSrv; +using LibHac.FsSystem; +using LibHac.Ncm; +using LibHac.Spl; +using LibHac.Tools.Es; +using LibHac.Tools.Fs; +using LibHac.Tools.FsSystem; +using Ryujinx.Common.Configuration; +using Ryujinx.Common.Logging; +using Ryujinx.HLE.HOS; +using System; +using System.Buffers.Text; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.IO; +using System.Runtime.CompilerServices; +using Path = System.IO.Path; +using RightsId = LibHac.Fs.RightsId; + +namespace Ryujinx.HLE.FileSystem +{ + public class VirtualFileSystem : IDisposable + { + public static string SafeNandPath = Path.Combine(AppDataManager.DefaultNandDir, "safe"); + public static string SystemNandPath = Path.Combine(AppDataManager.DefaultNandDir, "system"); + public static string UserNandPath = Path.Combine(AppDataManager.DefaultNandDir, "user"); + + public KeySet KeySet { get; private set; } + public EmulatedGameCard GameCard { get; private set; } + public EmulatedSdCard SdCard { get; private set; } + public ModLoader ModLoader { get; private set; } + + private readonly ConcurrentDictionary<ulong, Stream> _romFsByPid; + + private static bool _isInitialized = false; + + public static VirtualFileSystem CreateInstance() + { + if (_isInitialized) + { + throw new InvalidOperationException("VirtualFileSystem can only be instantiated once!"); + } + + _isInitialized = true; + + return new VirtualFileSystem(); + } + + private VirtualFileSystem() + { + ReloadKeySet(); + ModLoader = new ModLoader(); // Should only be created once + _romFsByPid = new ConcurrentDictionary<ulong, Stream>(); + } + + public void LoadRomFs(ulong pid, string fileName) + { + var romfsStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); + + _romFsByPid.AddOrUpdate(pid, romfsStream, (pid, oldStream) => + { + oldStream.Close(); + + return romfsStream; + }); + } + + public void SetRomFs(ulong pid, Stream romfsStream) + { + _romFsByPid.AddOrUpdate(pid, romfsStream, (pid, oldStream) => + { + oldStream.Close(); + + return romfsStream; + }); + } + + public Stream GetRomFs(ulong pid) + { + return _romFsByPid[pid]; + } + + public string GetFullPath(string basePath, string fileName) + { + if (fileName.StartsWith("//")) + { + fileName = fileName.Substring(2); + } + else if (fileName.StartsWith('/')) + { + fileName = fileName.Substring(1); + } + else + { + return null; + } + + string fullPath = Path.GetFullPath(Path.Combine(basePath, fileName)); + + if (!fullPath.StartsWith(AppDataManager.BaseDirPath)) + { + return null; + } + + return fullPath; + } + + internal string GetSdCardPath() => MakeFullPath(AppDataManager.DefaultSdcardDir); + public string GetNandPath() => MakeFullPath(AppDataManager.DefaultNandDir); + + public string SwitchPathToSystemPath(string switchPath) + { + string[] parts = switchPath.Split(":"); + + if (parts.Length != 2) + { + return null; + } + + return GetFullPath(MakeFullPath(parts[0]), parts[1]); + } + + public string SystemPathToSwitchPath(string systemPath) + { + string baseSystemPath = AppDataManager.BaseDirPath + Path.DirectorySeparatorChar; + + if (systemPath.StartsWith(baseSystemPath)) + { + string rawPath = systemPath.Replace(baseSystemPath, ""); + int firstSeparatorOffset = rawPath.IndexOf(Path.DirectorySeparatorChar); + + if (firstSeparatorOffset == -1) + { + return $"{rawPath}:/"; + } + + var basePath = rawPath.AsSpan(0, firstSeparatorOffset); + var fileName = rawPath.AsSpan(firstSeparatorOffset + 1); + + return $"{basePath}:/{fileName}"; + } + + return null; + } + + private string MakeFullPath(string path, bool isDirectory = true) + { + // Handles Common Switch Content Paths + switch (path) + { + case ContentPath.SdCard: + path = AppDataManager.DefaultSdcardDir; + break; + case ContentPath.User: + path = UserNandPath; + break; + case ContentPath.System: + path = SystemNandPath; + break; + case ContentPath.SdCardContent: + path = Path.Combine(AppDataManager.DefaultSdcardDir, "Nintendo", "Contents"); + break; + case ContentPath.UserContent: + path = Path.Combine(UserNandPath, "Contents"); + break; + case ContentPath.SystemContent: + path = Path.Combine(SystemNandPath, "Contents"); + break; + } + + string fullPath = Path.Combine(AppDataManager.BaseDirPath, path); + + if (isDirectory && !Directory.Exists(fullPath)) + { + Directory.CreateDirectory(fullPath); + } + + return fullPath; + } + + public void InitializeFsServer(LibHac.Horizon horizon, out HorizonClient fsServerClient) + { + LocalFileSystem serverBaseFs = new LocalFileSystem(AppDataManager.BaseDirPath); + + fsServerClient = horizon.CreatePrivilegedHorizonClient(); + var fsServer = new FileSystemServer(fsServerClient); + + RandomDataGenerator randomGenerator = Random.Shared.NextBytes; + + DefaultFsServerObjects fsServerObjects = DefaultFsServerObjects.GetDefaultEmulatedCreators(serverBaseFs, KeySet, fsServer, randomGenerator); + + // Use our own encrypted fs creator that doesn't actually do any encryption + fsServerObjects.FsCreators.EncryptedFileSystemCreator = new EncryptedFileSystemCreator(); + + GameCard = fsServerObjects.GameCard; + SdCard = fsServerObjects.SdCard; + + SdCard.SetSdCardInsertionStatus(true); + + var fsServerConfig = new FileSystemServerConfig + { + DeviceOperator = fsServerObjects.DeviceOperator, + ExternalKeySet = KeySet.ExternalKeySet, + FsCreators = fsServerObjects.FsCreators, + RandomGenerator = randomGenerator + }; + + FileSystemServerInitializer.InitializeWithConfig(fsServerClient, fsServer, fsServerConfig); + } + + public void ReloadKeySet() + { + KeySet ??= KeySet.CreateDefaultKeySet(); + + string keyFile = null; + string titleKeyFile = null; + string consoleKeyFile = null; + + if (AppDataManager.Mode == AppDataManager.LaunchMode.UserProfile) + { + LoadSetAtPath(AppDataManager.KeysDirPathUser); + } + + LoadSetAtPath(AppDataManager.KeysDirPath); + + void LoadSetAtPath(string basePath) + { + string localKeyFile = Path.Combine(basePath, "prod.keys"); + string localTitleKeyFile = Path.Combine(basePath, "title.keys"); + string localConsoleKeyFile = Path.Combine(basePath, "console.keys"); + + if (File.Exists(localKeyFile)) + { + keyFile = localKeyFile; + } + + if (File.Exists(localTitleKeyFile)) + { + titleKeyFile = localTitleKeyFile; + } + + if (File.Exists(localConsoleKeyFile)) + { + consoleKeyFile = localConsoleKeyFile; + } + } + + ExternalKeyReader.ReadKeyFile(KeySet, keyFile, titleKeyFile, consoleKeyFile, null); + } + + public void ImportTickets(IFileSystem fs) + { + foreach (DirectoryEntryEx ticketEntry in fs.EnumerateEntries("/", "*.tik")) + { + using var ticketFile = new UniqueRef<IFile>(); + + Result result = fs.OpenFile(ref ticketFile.Ref, ticketEntry.FullPath.ToU8Span(), OpenMode.Read); + + if (result.IsSuccess()) + { + Ticket ticket = new(ticketFile.Get.AsStream()); + var titleKey = ticket.GetTitleKey(KeySet); + + if (titleKey != null) + { + KeySet.ExternalKeySet.Add(new RightsId(ticket.RightsId), new AccessKey(titleKey)); + } + } + } + } + + // Save data created before we supported extra data in directory save data will not work properly if + // given empty extra data. Luckily some of that extra data can be created using the data from the + // save data indexer, which should be enough to check access permissions for user saves. + // Every single save data's extra data will be checked and fixed if needed each time the emulator is opened. + // Consider removing this at some point in the future when we don't need to worry about old saves. + public static Result FixExtraData(HorizonClient hos) + { + Result rc = GetSystemSaveList(hos, out List<ulong> systemSaveIds); + if (rc.IsFailure()) return rc; + + rc = FixUnindexedSystemSaves(hos, systemSaveIds); + if (rc.IsFailure()) return rc; + + rc = FixExtraDataInSpaceId(hos, SaveDataSpaceId.System); + if (rc.IsFailure()) return rc; + + rc = FixExtraDataInSpaceId(hos, SaveDataSpaceId.User); + if (rc.IsFailure()) return rc; + + return Result.Success; + } + + private static Result FixExtraDataInSpaceId(HorizonClient hos, SaveDataSpaceId spaceId) + { + Span<SaveDataInfo> info = stackalloc SaveDataInfo[8]; + + using var iterator = new UniqueRef<SaveDataIterator>(); + + Result rc = hos.Fs.OpenSaveDataIterator(ref iterator.Ref, spaceId); + if (rc.IsFailure()) return rc; + + while (true) + { + rc = iterator.Get.ReadSaveDataInfo(out long count, info); + if (rc.IsFailure()) return rc; + + if (count == 0) + return Result.Success; + + for (int i = 0; i < count; i++) + { + rc = FixExtraData(out bool wasFixNeeded, hos, in info[i]); + + if (ResultFs.TargetNotFound.Includes(rc)) + { + // If the save wasn't found, try to create the directory for its save data ID + rc = CreateSaveDataDirectory(hos, in info[i]); + + if (rc.IsFailure()) + { + Logger.Warning?.Print(LogClass.Application, $"Error {rc.ToStringWithName()} when creating save data 0x{info[i].SaveDataId:x} in the {spaceId} save data space"); + + // Don't bother fixing the extra data if we couldn't create the directory + continue; + } + + Logger.Info?.Print(LogClass.Application, $"Recreated directory for save data 0x{info[i].SaveDataId:x} in the {spaceId} save data space"); + + // Try to fix the extra data in the new directory + rc = FixExtraData(out wasFixNeeded, hos, in info[i]); + } + + if (rc.IsFailure()) + { + Logger.Warning?.Print(LogClass.Application, $"Error {rc.ToStringWithName()} when fixing extra data for save data 0x{info[i].SaveDataId:x} in the {spaceId} save data space"); + } + else if (wasFixNeeded) + { + Logger.Info?.Print(LogClass.Application, $"Fixed extra data for save data 0x{info[i].SaveDataId:x} in the {spaceId} save data space"); + } + } + } + } + + private static Result CreateSaveDataDirectory(HorizonClient hos, in SaveDataInfo info) + { + if (info.SpaceId != SaveDataSpaceId.User && info.SpaceId != SaveDataSpaceId.System) + return Result.Success; + + const string mountName = "SaveDir"; + var mountNameU8 = mountName.ToU8Span(); + + BisPartitionId partitionId = info.SpaceId switch + { + SaveDataSpaceId.System => BisPartitionId.System, + SaveDataSpaceId.User => BisPartitionId.User, + _ => throw new ArgumentOutOfRangeException() + }; + + Result rc = hos.Fs.MountBis(mountNameU8, partitionId); + if (rc.IsFailure()) return rc; + try + { + var path = $"{mountName}:/save/{info.SaveDataId:x16}".ToU8Span(); + + rc = hos.Fs.GetEntryType(out _, path); + + if (ResultFs.PathNotFound.Includes(rc)) + { + rc = hos.Fs.CreateDirectory(path); + } + + return rc; + } + finally + { + hos.Fs.Unmount(mountNameU8); + } + } + + // Gets a list of all the save data files or directories in the system partition. + private static Result GetSystemSaveList(HorizonClient hos, out List<ulong> list) + { + list = null; + + var mountName = "system".ToU8Span(); + DirectoryHandle handle = default; + List<ulong> localList = new List<ulong>(); + + try + { + Result rc = hos.Fs.MountBis(mountName, BisPartitionId.System); + if (rc.IsFailure()) return rc; + + rc = hos.Fs.OpenDirectory(out handle, "system:/save".ToU8Span(), OpenDirectoryMode.All); + if (rc.IsFailure()) return rc; + + DirectoryEntry entry = new DirectoryEntry(); + + while (true) + { + rc = hos.Fs.ReadDirectory(out long readCount, SpanHelpers.AsSpan(ref entry), handle); + if (rc.IsFailure()) return rc; + + if (readCount == 0) + break; + + if (Utf8Parser.TryParse(entry.Name, out ulong saveDataId, out int bytesRead, 'x') && + bytesRead == 16 && (long)saveDataId < 0) + { + localList.Add(saveDataId); + } + } + + list = localList; + + return Result.Success; + } + finally + { + if (handle.IsValid) + { + hos.Fs.CloseDirectory(handle); + } + + if (hos.Fs.IsMounted(mountName)) + { + hos.Fs.Unmount(mountName); + } + } + } + + // Adds system save data that isn't in the save data indexer to the indexer and creates extra data for it. + // Only save data IDs added to SystemExtraDataFixInfo will be fixed. + private static Result FixUnindexedSystemSaves(HorizonClient hos, List<ulong> existingSaveIds) + { + foreach (var fixInfo in SystemExtraDataFixInfo) + { + if (!existingSaveIds.Contains(fixInfo.StaticSaveDataId)) + { + continue; + } + + Result rc = FixSystemExtraData(out bool wasFixNeeded, hos, in fixInfo); + + if (rc.IsFailure()) + { + Logger.Warning?.Print(LogClass.Application, + $"Error {rc.ToStringWithName()} when fixing extra data for system save data 0x{fixInfo.StaticSaveDataId:x}"); + } + else if (wasFixNeeded) + { + Logger.Info?.Print(LogClass.Application, + $"Tried to rebuild extra data for system save data 0x{fixInfo.StaticSaveDataId:x}"); + } + } + + return Result.Success; + } + + private static Result FixSystemExtraData(out bool wasFixNeeded, HorizonClient hos, in ExtraDataFixInfo info) + { + wasFixNeeded = true; + + Result rc = hos.Fs.Impl.ReadSaveDataFileSystemExtraData(out SaveDataExtraData extraData, info.StaticSaveDataId); + if (!rc.IsSuccess()) + { + if (!ResultFs.TargetNotFound.Includes(rc)) + return rc; + + // We'll reach this point only if the save data directory exists but it's not in the save data indexer. + // Creating the save will add it to the indexer while leaving its existing contents intact. + return hos.Fs.CreateSystemSaveData(info.StaticSaveDataId, UserId.InvalidId, info.OwnerId, info.DataSize, + info.JournalSize, info.Flags); + } + + if (extraData.Attribute.StaticSaveDataId != 0 && extraData.OwnerId != 0) + { + wasFixNeeded = false; + return Result.Success; + } + + extraData = new SaveDataExtraData + { + Attribute = { StaticSaveDataId = info.StaticSaveDataId }, + OwnerId = info.OwnerId, + Flags = info.Flags, + DataSize = info.DataSize, + JournalSize = info.JournalSize + }; + + // Make a mask for writing the entire extra data + Unsafe.SkipInit(out SaveDataExtraData extraDataMask); + SpanHelpers.AsByteSpan(ref extraDataMask).Fill(0xFF); + + return hos.Fs.Impl.WriteSaveDataFileSystemExtraData(SaveDataSpaceId.System, info.StaticSaveDataId, + in extraData, in extraDataMask); + } + + private static Result FixExtraData(out bool wasFixNeeded, HorizonClient hos, in SaveDataInfo info) + { + wasFixNeeded = true; + + Result rc = hos.Fs.Impl.ReadSaveDataFileSystemExtraData(out SaveDataExtraData extraData, info.SpaceId, + info.SaveDataId); + if (rc.IsFailure()) return rc; + + // The extra data should have program ID or static save data ID set if it's valid. + // We only try to fix the extra data if the info from the save data indexer has a program ID or static save data ID. + bool canFixByProgramId = extraData.Attribute.ProgramId == ProgramId.InvalidId && + info.ProgramId != ProgramId.InvalidId; + + bool canFixBySaveDataId = extraData.Attribute.StaticSaveDataId == 0 && info.StaticSaveDataId != 0; + + bool hasEmptyOwnerId = extraData.OwnerId == 0 && info.Type != SaveDataType.System; + + if (!canFixByProgramId && !canFixBySaveDataId && !hasEmptyOwnerId) + { + wasFixNeeded = false; + return Result.Success; + } + + // The save data attribute struct can be completely created from the save data info. + extraData.Attribute.ProgramId = info.ProgramId; + extraData.Attribute.UserId = info.UserId; + extraData.Attribute.StaticSaveDataId = info.StaticSaveDataId; + extraData.Attribute.Type = info.Type; + extraData.Attribute.Rank = info.Rank; + extraData.Attribute.Index = info.Index; + + // The rest of the extra data can't be created from the save data info. + // On user saves the owner ID will almost certainly be the same as the program ID. + if (info.Type != SaveDataType.System) + { + extraData.OwnerId = info.ProgramId.Value; + } + else + { + // Try to match the system save with one of the known saves + foreach (ExtraDataFixInfo fixInfo in SystemExtraDataFixInfo) + { + if (extraData.Attribute.StaticSaveDataId == fixInfo.StaticSaveDataId) + { + extraData.OwnerId = fixInfo.OwnerId; + extraData.Flags = fixInfo.Flags; + extraData.DataSize = fixInfo.DataSize; + extraData.JournalSize = fixInfo.JournalSize; + + break; + } + } + } + + // Make a mask for writing the entire extra data + Unsafe.SkipInit(out SaveDataExtraData extraDataMask); + SpanHelpers.AsByteSpan(ref extraDataMask).Fill(0xFF); + + return hos.Fs.Impl.WriteSaveDataFileSystemExtraData(info.SpaceId, info.SaveDataId, in extraData, in extraDataMask); + } + + struct ExtraDataFixInfo + { + public ulong StaticSaveDataId; + public ulong OwnerId; + public SaveDataFlags Flags; + public long DataSize; + public long JournalSize; + } + + private static readonly ExtraDataFixInfo[] SystemExtraDataFixInfo = + { + new ExtraDataFixInfo() + { + StaticSaveDataId = 0x8000000000000030, + OwnerId = 0x010000000000001F, + Flags = SaveDataFlags.KeepAfterResettingSystemSaveDataWithoutUserSaveData, + DataSize = 0x10000, + JournalSize = 0x10000 + }, + new ExtraDataFixInfo() + { + StaticSaveDataId = 0x8000000000001040, + OwnerId = 0x0100000000001009, + Flags = SaveDataFlags.None, + DataSize = 0xC000, + JournalSize = 0xC000 + } + }; + + public void Dispose() + { + Dispose(true); + } + + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + foreach (var stream in _romFsByPid.Values) + { + stream.Close(); + } + + _romFsByPid.Clear(); + } + } + } +}
\ No newline at end of file |