2020-01-12 03:10:55 +01:00
|
|
|
using LibHac;
|
2020-03-25 09:14:35 +01:00
|
|
|
using LibHac.Common;
|
2020-01-12 03:10:55 +01:00
|
|
|
using LibHac.Fs;
|
2020-09-01 22:08:59 +02:00
|
|
|
using LibHac.Fs.Fsa;
|
2020-01-12 03:10:55 +01:00
|
|
|
using LibHac.FsSystem;
|
2019-10-17 08:17:44 +02:00
|
|
|
using LibHac.FsSystem.NcaUtils;
|
2020-01-12 03:10:55 +01:00
|
|
|
using LibHac.Ncm;
|
2020-06-20 19:38:14 +02:00
|
|
|
using Ryujinx.Common.Logging;
|
2020-01-12 03:10:55 +01:00
|
|
|
using Ryujinx.HLE.Exceptions;
|
2019-10-08 05:48:49 +02:00
|
|
|
using Ryujinx.HLE.HOS.Services.Time;
|
2018-11-18 20:37:41 +01:00
|
|
|
using Ryujinx.HLE.Utilities;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
2020-01-12 03:10:55 +01:00
|
|
|
using System.IO.Compression;
|
2018-11-18 20:37:41 +01:00
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.FileSystem.Content
|
|
|
|
{
|
2020-01-21 23:23:11 +01:00
|
|
|
public class ContentManager
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2020-01-12 03:10:55 +01:00
|
|
|
private const ulong SystemVersionTitleId = 0x0100000000000809;
|
|
|
|
private const ulong SystemUpdateTitleId = 0x0100000000000816;
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private Dictionary<StorageId, LinkedList<LocationEntry>> _locationEntries;
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
private Dictionary<string, ulong> _sharedFontTitleDictionary;
|
|
|
|
private Dictionary<ulong, string> _systemTitlesNameDictionary;
|
2019-06-01 02:31:10 +02:00
|
|
|
private Dictionary<string, string> _sharedFontFilenameDictionary;
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-12 03:10:55 +01:00
|
|
|
private SortedDictionary<(ulong titleId, NcaContentType type), string> _contentDictionary;
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-06-20 19:38:14 +02:00
|
|
|
private struct AocItem
|
|
|
|
{
|
|
|
|
public readonly string ContainerPath;
|
|
|
|
public readonly string NcaPath;
|
|
|
|
public bool Enabled;
|
|
|
|
|
|
|
|
public AocItem(string containerPath, string ncaPath, bool enabled)
|
|
|
|
{
|
|
|
|
ContainerPath = containerPath;
|
|
|
|
NcaPath = ncaPath;
|
|
|
|
Enabled = enabled;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private SortedList<ulong, AocItem> _aocData { get; }
|
|
|
|
|
2020-01-21 23:23:11 +01:00
|
|
|
private VirtualFileSystem _virtualFileSystem;
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
private readonly object _lock = new object();
|
|
|
|
|
2020-01-21 23:23:11 +01:00
|
|
|
public ContentManager(VirtualFileSystem virtualFileSystem)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2019-10-17 08:17:44 +02:00
|
|
|
_contentDictionary = new SortedDictionary<(ulong, NcaContentType), string>();
|
2018-12-06 12:16:24 +01:00
|
|
|
_locationEntries = new Dictionary<StorageId, LinkedList<LocationEntry>>();
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
_sharedFontTitleDictionary = new Dictionary<string, ulong>
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
|
|
|
{ "FontStandard", 0x0100000000000811 },
|
|
|
|
{ "FontChineseSimplified", 0x0100000000000814 },
|
|
|
|
{ "FontExtendedChineseSimplified", 0x0100000000000814 },
|
|
|
|
{ "FontKorean", 0x0100000000000812 },
|
|
|
|
{ "FontChineseTraditional", 0x0100000000000813 },
|
2018-12-06 12:16:24 +01:00
|
|
|
{ "FontNintendoExtended", 0x0100000000000810 }
|
2018-11-18 20:37:41 +01:00
|
|
|
};
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
_systemTitlesNameDictionary = new Dictionary<ulong, string>()
|
2020-03-29 23:23:05 +02:00
|
|
|
{
|
|
|
|
{ 0x010000000000080E, "TimeZoneBinary" },
|
|
|
|
{ 0x0100000000000810, "FontNintendoExtension" },
|
|
|
|
{ 0x0100000000000811, "FontStandard" },
|
|
|
|
{ 0x0100000000000812, "FontKorean" },
|
|
|
|
{ 0x0100000000000813, "FontChineseTraditional" },
|
|
|
|
{ 0x0100000000000814, "FontChineseSimple" },
|
|
|
|
};
|
|
|
|
|
2019-06-01 02:31:10 +02:00
|
|
|
_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" }
|
|
|
|
};
|
|
|
|
|
2020-01-21 23:23:11 +01:00
|
|
|
_virtualFileSystem = virtualFileSystem;
|
2020-06-20 19:38:14 +02:00
|
|
|
|
|
|
|
_aocData = new SortedList<ulong, AocItem>();
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
|
|
|
|
2020-01-21 23:23:11 +01:00
|
|
|
public void LoadEntries(Switch device = null)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2020-01-13 01:17:44 +01:00
|
|
|
lock (_lock)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2020-01-13 01:17:44 +01:00
|
|
|
_contentDictionary = new SortedDictionary<(ulong, NcaContentType), string>();
|
|
|
|
_locationEntries = new Dictionary<StorageId, LinkedList<LocationEntry>>();
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
foreach (StorageId storageId in Enum.GetValues(typeof(StorageId)))
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2020-01-13 01:17:44 +01:00
|
|
|
string contentDirectory = null;
|
|
|
|
string contentPathString = null;
|
|
|
|
string registeredDirectory = null;
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
contentPathString = LocationHelper.GetContentRoot(storageId);
|
2020-01-21 23:23:11 +01:00
|
|
|
contentDirectory = LocationHelper.GetRealPath(_virtualFileSystem, contentPathString);
|
2020-01-13 01:17:44 +01:00
|
|
|
registeredDirectory = Path.Combine(contentDirectory, "registered");
|
|
|
|
}
|
|
|
|
catch (NotSupportedException)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
Directory.CreateDirectory(registeredDirectory);
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
LinkedList<LocationEntry> locationList = new LinkedList<LocationEntry>();
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
void AddEntry(LocationEntry entry)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2020-01-13 01:17:44 +01:00
|
|
|
locationList.AddLast(entry);
|
|
|
|
}
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
foreach (string directoryPath in Directory.EnumerateDirectories(registeredDirectory))
|
|
|
|
{
|
|
|
|
if (Directory.GetFiles(directoryPath).Length > 0)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2020-01-13 01:17:44 +01:00
|
|
|
string ncaName = new DirectoryInfo(directoryPath).Name.Replace(".nca", string.Empty);
|
|
|
|
|
|
|
|
using (FileStream ncaFile = File.OpenRead(Directory.GetFiles(directoryPath)[0]))
|
|
|
|
{
|
2020-01-21 23:23:11 +01:00
|
|
|
Nca nca = new Nca(_virtualFileSystem.KeySet, ncaFile.AsStorage());
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
string switchPath = contentPathString + ":/" + ncaFile.Name.Replace(contentDirectory, string.Empty).TrimStart(Path.DirectorySeparatorChar);
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
// Change path format to switch's
|
|
|
|
switchPath = switchPath.Replace('\\', '/');
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
LocationEntry entry = new LocationEntry(switchPath,
|
|
|
|
0,
|
2021-04-24 12:16:01 +02:00
|
|
|
nca.Header.TitleId,
|
2020-01-13 01:17:44 +01:00
|
|
|
nca.Header.ContentType);
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
AddEntry(entry);
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
_contentDictionary.Add((nca.Header.TitleId, nca.Header.ContentType), ncaName);
|
|
|
|
}
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
foreach (string filePath in Directory.EnumerateFiles(contentDirectory))
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2020-01-13 01:17:44 +01:00
|
|
|
if (Path.GetExtension(filePath) == ".nca")
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2020-01-13 01:17:44 +01:00
|
|
|
string ncaName = Path.GetFileNameWithoutExtension(filePath);
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
using (FileStream ncaFile = new FileStream(filePath, FileMode.Open, FileAccess.Read))
|
|
|
|
{
|
2020-01-21 23:23:11 +01:00
|
|
|
Nca nca = new Nca(_virtualFileSystem.KeySet, ncaFile.AsStorage());
|
2020-01-13 01:17:44 +01:00
|
|
|
|
|
|
|
string switchPath = contentPathString + ":/" + filePath.Replace(contentDirectory, string.Empty).TrimStart(Path.DirectorySeparatorChar);
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
// Change path format to switch's
|
|
|
|
switchPath = switchPath.Replace('\\', '/');
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
LocationEntry entry = new LocationEntry(switchPath,
|
|
|
|
0,
|
2021-04-24 12:16:01 +02:00
|
|
|
nca.Header.TitleId,
|
2020-01-13 01:17:44 +01:00
|
|
|
nca.Header.ContentType);
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
AddEntry(entry);
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
_contentDictionary.Add((nca.Header.TitleId, nca.Header.ContentType), ncaName);
|
|
|
|
}
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
if (_locationEntries.ContainsKey(storageId) && _locationEntries[storageId]?.Count == 0)
|
|
|
|
{
|
|
|
|
_locationEntries.Remove(storageId);
|
|
|
|
}
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
if (!_locationEntries.ContainsKey(storageId))
|
|
|
|
{
|
|
|
|
_locationEntries.Add(storageId, locationList);
|
|
|
|
}
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
2019-07-04 17:20:40 +02:00
|
|
|
|
2020-01-21 23:23:11 +01:00
|
|
|
if (device != null)
|
|
|
|
{
|
|
|
|
TimeManager.Instance.InitializeTimeZone(device);
|
|
|
|
device.System.Font.Initialize(this);
|
|
|
|
}
|
2020-01-13 01:17:44 +01:00
|
|
|
}
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
|
|
|
|
2020-06-20 19:38:14 +02:00
|
|
|
// fs must contain AOC nca files in its root
|
2021-05-16 17:12:14 +02:00
|
|
|
public void AddAocData(IFileSystem fs, string containerPath, ulong aocBaseId, IntegrityCheckLevel integrityCheckLevel)
|
2020-06-20 19:38:14 +02:00
|
|
|
{
|
|
|
|
_virtualFileSystem.ImportTickets(fs);
|
|
|
|
|
|
|
|
foreach (var ncaPath in fs.EnumerateEntries("*.cnmt.nca", SearchOptions.Default))
|
|
|
|
{
|
|
|
|
fs.OpenFile(out IFile ncaFile, ncaPath.FullPath.ToU8Span(), OpenMode.Read);
|
|
|
|
using (ncaFile)
|
|
|
|
{
|
|
|
|
var nca = new Nca(_virtualFileSystem.KeySet, ncaFile.AsStorage());
|
|
|
|
if (nca.Header.ContentType != NcaContentType.Meta)
|
|
|
|
{
|
2020-08-04 01:32:53 +02:00
|
|
|
Logger.Warning?.Print(LogClass.Application, $"{ncaPath} is not a valid metadata file");
|
2020-06-20 19:38:14 +02:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-16 17:12:14 +02:00
|
|
|
using var pfs0 = nca.OpenFileSystem(0, integrityCheckLevel);
|
2020-06-20 19:38:14 +02:00
|
|
|
|
|
|
|
pfs0.OpenFile(out IFile cnmtFile, pfs0.EnumerateEntries().Single().FullPath.ToU8Span(), OpenMode.Read);
|
|
|
|
|
|
|
|
using (cnmtFile)
|
|
|
|
{
|
|
|
|
var cnmt = new Cnmt(cnmtFile.AsStream());
|
|
|
|
|
|
|
|
if (cnmt.Type != ContentMetaType.AddOnContent || (cnmt.TitleId & 0xFFFFFFFFFFFFE000) != aocBaseId)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
string ncaId = BitConverter.ToString(cnmt.ContentEntries[0].NcaId).Replace("-", "").ToLower();
|
|
|
|
if (!_aocData.TryAdd(cnmt.TitleId, new AocItem(containerPath, $"{ncaId}.nca", true)))
|
|
|
|
{
|
2020-08-04 01:32:53 +02:00
|
|
|
Logger.Warning?.Print(LogClass.Application, $"Duplicate AddOnContent detected. TitleId {cnmt.TitleId:X16}");
|
2020-06-20 19:38:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-04 01:32:53 +02:00
|
|
|
Logger.Info?.Print(LogClass.Application, $"Found AddOnContent with TitleId {cnmt.TitleId:X16}");
|
2020-06-20 19:38:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 02:32:07 +02:00
|
|
|
public void AddAocItem(ulong titleId, string containerPath, string ncaPath, bool enabled)
|
|
|
|
{
|
|
|
|
if (!_aocData.TryAdd(titleId, new AocItem(containerPath, ncaPath, enabled)))
|
|
|
|
{
|
2020-08-04 01:32:53 +02:00
|
|
|
Logger.Warning?.Print(LogClass.Application, $"Duplicate AddOnContent detected. TitleId {titleId:X16}");
|
2020-06-23 02:32:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-04 01:32:53 +02:00
|
|
|
Logger.Info?.Print(LogClass.Application, $"Found AddOnContent with TitleId {titleId:X16}");
|
2020-06-23 13:25:12 +02:00
|
|
|
|
|
|
|
using (FileStream fileStream = File.OpenRead(containerPath))
|
|
|
|
using (PartitionFileSystem pfs = new PartitionFileSystem(fileStream.AsStorage()))
|
|
|
|
{
|
|
|
|
_virtualFileSystem.ImportTickets(pfs);
|
|
|
|
}
|
2020-06-23 02:32:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 19:38:14 +02:00
|
|
|
public void ClearAocData() => _aocData.Clear();
|
|
|
|
|
|
|
|
public int GetAocCount() => _aocData.Where(e => e.Value.Enabled).Count();
|
|
|
|
|
|
|
|
public IList<ulong> GetAocTitleIds() => _aocData.Where(e => e.Value.Enabled).Select(e => e.Key).ToList();
|
|
|
|
|
2021-05-16 17:12:14 +02:00
|
|
|
public bool GetAocDataStorage(ulong aocTitleId, out IStorage aocStorage, IntegrityCheckLevel integrityCheckLevel)
|
2020-06-20 19:38:14 +02:00
|
|
|
{
|
|
|
|
aocStorage = null;
|
|
|
|
|
|
|
|
if (_aocData.TryGetValue(aocTitleId, out AocItem aoc) && aoc.Enabled)
|
|
|
|
{
|
|
|
|
var file = new FileStream(aoc.ContainerPath, FileMode.Open, FileAccess.Read);
|
|
|
|
PartitionFileSystem pfs;
|
|
|
|
IFile ncaFile;
|
|
|
|
|
|
|
|
switch (Path.GetExtension(aoc.ContainerPath))
|
|
|
|
{
|
|
|
|
case ".xci":
|
|
|
|
pfs = new Xci(_virtualFileSystem.KeySet, file.AsStorage()).OpenPartition(XciPartitionType.Secure);
|
|
|
|
pfs.OpenFile(out ncaFile, aoc.NcaPath.ToU8Span(), OpenMode.Read);
|
|
|
|
break;
|
|
|
|
case ".nsp":
|
|
|
|
pfs = new PartitionFileSystem(file.AsStorage());
|
|
|
|
pfs.OpenFile(out ncaFile, aoc.NcaPath.ToU8Span(), OpenMode.Read);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false; // Print error?
|
|
|
|
}
|
|
|
|
|
2021-05-16 17:12:14 +02:00
|
|
|
aocStorage = new Nca(_virtualFileSystem.KeySet, ncaFile.AsStorage()).OpenStorage(NcaSectionType.Data, integrityCheckLevel);
|
2020-06-20 19:38:14 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
public void ClearEntry(ulong titleId, NcaContentType contentType, StorageId storageId)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2020-01-13 01:17:44 +01:00
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
RemoveLocationEntry(titleId, contentType, storageId);
|
|
|
|
}
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public void RefreshEntries(StorageId storageId, int flag)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2020-01-13 01:17:44 +01:00
|
|
|
lock (_lock)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2020-01-13 01:17:44 +01:00
|
|
|
LinkedList<LocationEntry> locationList = _locationEntries[storageId];
|
|
|
|
LinkedListNode<LocationEntry> locationEntry = locationList.First;
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
while (locationEntry != null)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2020-01-13 01:17:44 +01:00
|
|
|
LinkedListNode<LocationEntry> nextLocationEntry = locationEntry.Next;
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
if (locationEntry.Value.Flag == flag)
|
|
|
|
{
|
|
|
|
locationList.Remove(locationEntry.Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
locationEntry = nextLocationEntry;
|
|
|
|
}
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public bool HasNca(string ncaId, StorageId storageId)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2020-01-13 01:17:44 +01:00
|
|
|
lock (_lock)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2020-01-13 01:17:44 +01:00
|
|
|
if (_contentDictionary.ContainsValue(ncaId))
|
|
|
|
{
|
|
|
|
var content = _contentDictionary.FirstOrDefault(x => x.Value == ncaId);
|
2021-04-24 12:16:01 +02:00
|
|
|
ulong titleId = content.Key.Item1;
|
2020-01-12 03:10:55 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
NcaContentType contentType = content.Key.type;
|
|
|
|
StorageId storage = GetInstalledStorage(titleId, contentType, storageId);
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
return storage == storageId;
|
|
|
|
}
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
public UInt128 GetInstalledNcaId(ulong titleId, NcaContentType contentType)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2020-01-13 01:17:44 +01:00
|
|
|
lock (_lock)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2021-04-24 12:16:01 +02:00
|
|
|
if (_contentDictionary.ContainsKey((titleId, contentType)))
|
2020-01-13 01:17:44 +01:00
|
|
|
{
|
2021-04-24 12:16:01 +02:00
|
|
|
return new UInt128(_contentDictionary[(titleId, contentType)]);
|
2020-01-13 01:17:44 +01:00
|
|
|
}
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return new UInt128();
|
|
|
|
}
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
public StorageId GetInstalledStorage(ulong titleId, NcaContentType contentType, StorageId storageId)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2020-01-13 01:17:44 +01:00
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
LocationEntry locationEntry = GetLocation(titleId, contentType, storageId);
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
return locationEntry.ContentPath != null ?
|
|
|
|
LocationHelper.GetStorageId(locationEntry.ContentPath) : StorageId.None;
|
|
|
|
}
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
public string GetInstalledContentPath(ulong titleId, StorageId storageId, NcaContentType contentType)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2020-01-13 01:17:44 +01:00
|
|
|
lock (_lock)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2020-01-13 01:17:44 +01:00
|
|
|
LocationEntry locationEntry = GetLocation(titleId, contentType, storageId);
|
|
|
|
|
|
|
|
if (VerifyContentType(locationEntry, contentType))
|
|
|
|
{
|
|
|
|
return locationEntry.ContentPath;
|
|
|
|
}
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return string.Empty;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public void RedirectLocation(LocationEntry newEntry, StorageId storageId)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2020-01-13 01:17:44 +01:00
|
|
|
lock (_lock)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2020-01-13 01:17:44 +01:00
|
|
|
LocationEntry locationEntry = GetLocation(newEntry.TitleId, newEntry.ContentType, storageId);
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
if (locationEntry.ContentPath != null)
|
|
|
|
{
|
|
|
|
RemoveLocationEntry(newEntry.TitleId, newEntry.ContentType, storageId);
|
|
|
|
}
|
|
|
|
|
|
|
|
AddLocationEntry(newEntry, storageId);
|
|
|
|
}
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
|
|
|
|
2019-10-17 08:17:44 +02:00
|
|
|
private bool VerifyContentType(LocationEntry locationEntry, NcaContentType contentType)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (locationEntry.ContentPath == null)
|
2018-11-19 01:20:17 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-12 03:10:55 +01:00
|
|
|
|
2020-01-21 23:23:11 +01:00
|
|
|
string installedPath = _virtualFileSystem.SwitchPathToSystemPath(locationEntry.ContentPath);
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!string.IsNullOrWhiteSpace(installedPath))
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (File.Exists(installedPath))
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2019-01-05 01:41:49 +01:00
|
|
|
using (FileStream file = new FileStream(installedPath, FileMode.Open, FileAccess.Read))
|
|
|
|
{
|
2020-01-21 23:23:11 +01:00
|
|
|
Nca nca = new Nca(_virtualFileSystem.KeySet, file.AsStorage());
|
2019-01-05 01:41:49 +01:00
|
|
|
bool contentCheck = nca.Header.ContentType == contentType;
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2019-01-05 01:41:49 +01:00
|
|
|
return contentCheck;
|
|
|
|
}
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private void AddLocationEntry(LocationEntry entry, StorageId storageId)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
LinkedList<LocationEntry> locationList = null;
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (_locationEntries.ContainsKey(storageId))
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
locationList = _locationEntries[storageId];
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (locationList != null)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (locationList.Contains(entry))
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
locationList.Remove(entry);
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
locationList.AddLast(entry);
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
private void RemoveLocationEntry(ulong titleId, NcaContentType contentType, StorageId storageId)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
LinkedList<LocationEntry> locationList = null;
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (_locationEntries.ContainsKey(storageId))
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
locationList = _locationEntries[storageId];
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (locationList != null)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
LocationEntry entry =
|
|
|
|
locationList.ToList().Find(x => x.TitleId == titleId && x.ContentType == contentType);
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (entry.ContentPath != null)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
locationList.Remove(entry);
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
public bool TryGetFontTitle(string fontName, out ulong titleId)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return _sharedFontTitleDictionary.TryGetValue(fontName, out titleId);
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
|
|
|
|
2019-06-01 02:31:10 +02:00
|
|
|
public bool TryGetFontFilename(string fontName, out string filename)
|
|
|
|
{
|
|
|
|
return _sharedFontFilenameDictionary.TryGetValue(fontName, out filename);
|
|
|
|
}
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
public bool TryGetSystemTitlesName(ulong titleId, out string name)
|
2020-03-29 23:23:05 +02:00
|
|
|
{
|
|
|
|
return _systemTitlesNameDictionary.TryGetValue(titleId, out name);
|
|
|
|
}
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
private LocationEntry GetLocation(ulong titleId, NcaContentType contentType, StorageId storageId)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
LinkedList<LocationEntry> locationList = _locationEntries[storageId];
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return locationList.ToList().Find(x => x.TitleId == titleId && x.ContentType == contentType);
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
2020-01-12 03:10:55 +01:00
|
|
|
|
|
|
|
public void InstallFirmware(string firmwareSource)
|
|
|
|
{
|
|
|
|
string contentPathString = LocationHelper.GetContentRoot(StorageId.NandSystem);
|
2020-01-21 23:23:11 +01:00
|
|
|
string contentDirectory = LocationHelper.GetRealPath(_virtualFileSystem, contentPathString);
|
2020-01-12 03:10:55 +01:00
|
|
|
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":
|
2020-01-21 23:23:11 +01:00
|
|
|
Xci xci = new Xci(_virtualFileSystem.KeySet, file.AsStorage());
|
2020-01-12 03:10:55 +01:00
|
|
|
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"))
|
|
|
|
{
|
2020-01-21 23:23:11 +01:00
|
|
|
Nca nca = new Nca(_virtualFileSystem.KeySet, OpenPossibleFragmentedFile(filesystem, entry.FullPath, OpenMode.Read).AsStorage());
|
2020-01-12 03:10:55 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
IFile file;
|
|
|
|
|
|
|
|
if (filesystem.FileExists($"{path}/00"))
|
|
|
|
{
|
2020-03-25 09:14:35 +01:00
|
|
|
filesystem.OpenFile(out file, $"{path}/00".ToU8Span(), mode);
|
2020-01-12 03:10:55 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-25 09:14:35 +01:00
|
|
|
filesystem.OpenFile(out file, path.ToU8Span(), mode);
|
2020-01-12 03:10:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Stream GetZipStream(ZipArchiveEntry entry)
|
|
|
|
{
|
|
|
|
MemoryStream dest = new MemoryStream();
|
|
|
|
|
|
|
|
Stream src = entry.Open();
|
|
|
|
|
|
|
|
src.CopyTo(dest);
|
|
|
|
src.Dispose();
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
public SystemVersion VerifyFirmwarePackage(string firmwarePackage)
|
|
|
|
{
|
2021-08-12 23:56:24 +02:00
|
|
|
_virtualFileSystem.ReloadKeySet();
|
2021-01-26 18:45:07 +01:00
|
|
|
|
|
|
|
// 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.
|
2021-08-12 23:56:24 +02:00
|
|
|
if (_virtualFileSystem.KeySet.HeaderKey.IsZeros())
|
2021-01-26 18:45:07 +01:00
|
|
|
{
|
|
|
|
throw new MissingKeyException("HeaderKey is empty. Cannot decrypt NCA headers.");
|
|
|
|
}
|
|
|
|
|
2020-01-12 03:10:55 +01:00
|
|
|
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":
|
2020-01-21 23:23:11 +01:00
|
|
|
Xci xci = new Xci(_virtualFileSystem.KeySet, file.AsStorage());
|
2020-01-12 03:10:55 +01:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2020-01-21 23:23:11 +01:00
|
|
|
Nca nca = new Nca(_virtualFileSystem.KeySet, storage);
|
2020-01-12 03:10:55 +01:00
|
|
|
|
|
|
|
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))
|
|
|
|
{
|
2020-01-21 23:23:11 +01:00
|
|
|
Nca metaNca = new Nca(_virtualFileSystem.KeySet, ncaStream.AsStorage());
|
2020-01-12 03:10:55 +01:00
|
|
|
|
2021-05-16 17:12:14 +02:00
|
|
|
IFileSystem fs = metaNca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid);
|
2020-01-12 03:10:55 +01:00
|
|
|
|
|
|
|
string cnmtPath = fs.EnumerateEntries("/", "*.cnmt").Single().FullPath;
|
|
|
|
|
2020-03-25 09:14:35 +01:00
|
|
|
if (fs.OpenFile(out IFile metaFile, cnmtPath.ToU8Span(), OpenMode.Read).IsSuccess())
|
2020-01-12 03:10:55 +01:00
|
|
|
{
|
|
|
|
var meta = new Cnmt(metaFile.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)))
|
|
|
|
{
|
2020-01-21 23:23:11 +01:00
|
|
|
Nca nca = new Nca(_virtualFileSystem.KeySet, ncaStream.AsStorage());
|
2020-01-12 03:10:55 +01:00
|
|
|
|
2021-05-16 17:12:14 +02:00
|
|
|
var romfs = nca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid);
|
2020-01-12 03:10:55 +01:00
|
|
|
|
2020-03-25 09:14:35 +01:00
|
|
|
if (romfs.OpenFile(out IFile systemVersionFile, "/file".ToU8Span(), OpenMode.Read).IsSuccess())
|
2020-01-12 03:10:55 +01:00
|
|
|
{
|
|
|
|
systemVersion = new SystemVersion(systemVersionFile.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))
|
|
|
|
{
|
2020-01-21 23:23:11 +01:00
|
|
|
Nca metaNca = new Nca(_virtualFileSystem.KeySet, metaNcaStream.AsStorage());
|
2020-01-12 03:10:55 +01:00
|
|
|
|
2021-05-16 17:12:14 +02:00
|
|
|
IFileSystem fs = metaNca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid);
|
2020-01-12 03:10:55 +01:00
|
|
|
|
|
|
|
string cnmtPath = fs.EnumerateEntries("/", "*.cnmt").Single().FullPath;
|
|
|
|
|
2020-03-25 09:14:35 +01:00
|
|
|
if (fs.OpenFile(out IFile metaFile, cnmtPath.ToU8Span(), OpenMode.Read).IsSuccess())
|
2020-01-12 03:10:55 +01:00
|
|
|
{
|
|
|
|
var meta = new Cnmt(metaFile.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);
|
|
|
|
|
2020-09-01 22:08:59 +02:00
|
|
|
if (LibHac.Utilities.ArraysEqual(hash.ToArray(), meta.ContentEntries[0].Hash))
|
2020-01-12 03:10:55 +01:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
2020-01-21 23:23:11 +01:00
|
|
|
Nca nca = new Nca(_virtualFileSystem.KeySet, ncaStorage);
|
2020-01-12 03:10:55 +01:00
|
|
|
|
|
|
|
if (nca.Header.TitleId == SystemUpdateTitleId && nca.Header.ContentType == NcaContentType.Meta)
|
|
|
|
{
|
2021-05-16 17:12:14 +02:00
|
|
|
IFileSystem fs = nca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid);
|
2020-01-12 03:10:55 +01:00
|
|
|
|
|
|
|
string cnmtPath = fs.EnumerateEntries("/", "*.cnmt").Single().FullPath;
|
|
|
|
|
2020-03-25 09:14:35 +01:00
|
|
|
if (fs.OpenFile(out IFile metaFile, cnmtPath.ToU8Span(), OpenMode.Read).IsSuccess())
|
2020-01-12 03:10:55 +01:00
|
|
|
{
|
|
|
|
var meta = new Cnmt(metaFile.AsStream());
|
|
|
|
|
|
|
|
if (meta.Type == ContentMetaType.SystemUpdate)
|
|
|
|
{
|
|
|
|
metaEntries = meta.MetaEntries;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (nca.Header.TitleId == SystemVersionTitleId && nca.Header.ContentType == NcaContentType.Data)
|
|
|
|
{
|
2021-05-16 17:12:14 +02:00
|
|
|
var romfs = nca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid);
|
2020-01-12 03:10:55 +01:00
|
|
|
|
2020-03-25 09:14:35 +01:00
|
|
|
if (romfs.OpenFile(out IFile systemVersionFile, "/file".ToU8Span(), OpenMode.Read).IsSuccess())
|
2020-01-12 03:10:55 +01:00
|
|
|
{
|
|
|
|
systemVersion = new SystemVersion(systemVersionFile.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();
|
|
|
|
|
2020-01-21 23:23:11 +01:00
|
|
|
Nca metaNca = new Nca(_virtualFileSystem.KeySet, metaStorage);
|
2020-01-12 03:10:55 +01:00
|
|
|
|
2021-05-16 17:12:14 +02:00
|
|
|
IFileSystem fs = metaNca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid);
|
2020-01-12 03:10:55 +01:00
|
|
|
|
|
|
|
string cnmtPath = fs.EnumerateEntries("/", "*.cnmt").Single().FullPath;
|
|
|
|
|
2020-03-25 09:14:35 +01:00
|
|
|
if (fs.OpenFile(out IFile metaFile, cnmtPath.ToU8Span(), OpenMode.Read).IsSuccess())
|
2020-01-12 03:10:55 +01:00
|
|
|
{
|
|
|
|
var meta = new Cnmt(metaFile.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);
|
|
|
|
|
2020-09-01 22:08:59 +02:00
|
|
|
if (LibHac.Utilities.ArraysEqual(hash.ToArray(), meta.ContentEntries[0].Hash))
|
2020-01-12 03:10:55 +01:00
|
|
|
{
|
|
|
|
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}");
|
|
|
|
}
|
|
|
|
|
|
|
|
return systemVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public SystemVersion GetCurrentFirmwareVersion()
|
|
|
|
{
|
2020-01-21 23:23:11 +01:00
|
|
|
LoadEntries();
|
2020-01-12 03:10:55 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
lock (_lock)
|
2020-01-12 03:10:55 +01:00
|
|
|
{
|
2020-01-13 01:17:44 +01:00
|
|
|
var locationEnties = _locationEntries[StorageId.NandSystem];
|
2020-01-12 03:10:55 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
foreach (var entry in locationEnties)
|
|
|
|
{
|
|
|
|
if (entry.ContentType == NcaContentType.Data)
|
2020-01-12 03:10:55 +01:00
|
|
|
{
|
2020-01-21 23:23:11 +01:00
|
|
|
var path = _virtualFileSystem.SwitchPathToSystemPath(entry.ContentPath);
|
2020-01-12 03:10:55 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
using (FileStream fileStream = File.OpenRead(path))
|
2020-01-12 03:10:55 +01:00
|
|
|
{
|
2020-01-21 23:23:11 +01:00
|
|
|
Nca nca = new Nca(_virtualFileSystem.KeySet, fileStream.AsStorage());
|
2020-01-12 03:10:55 +01:00
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
if (nca.Header.TitleId == SystemVersionTitleId && nca.Header.ContentType == NcaContentType.Data)
|
2020-01-12 03:10:55 +01:00
|
|
|
{
|
2021-05-16 17:12:14 +02:00
|
|
|
var romfs = nca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid);
|
2020-01-13 01:17:44 +01:00
|
|
|
|
2020-03-25 09:14:35 +01:00
|
|
|
if (romfs.OpenFile(out IFile systemVersionFile, "/file".ToU8Span(), OpenMode.Read).IsSuccess())
|
2020-01-13 01:17:44 +01:00
|
|
|
{
|
|
|
|
return new SystemVersion(systemVersionFile.AsStream());
|
|
|
|
}
|
2020-01-12 03:10:55 +01:00
|
|
|
}
|
|
|
|
|
2020-01-13 01:17:44 +01:00
|
|
|
}
|
2020-01-12 03:10:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
|
|
|
}
|