From 46d4274686fadd5f45bc36f8891bba512fffb984 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Sat, 22 Jun 2019 17:18:28 -0500 Subject: [PATCH] Support xts nca. Handle more errors in switchfs --- src/LibHac/Fs/NcaUtils/Nca.cs | 10 +++++- src/LibHac/SwitchFs.cs | 68 +++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 31 deletions(-) diff --git a/src/LibHac/Fs/NcaUtils/Nca.cs b/src/LibHac/Fs/NcaUtils/Nca.cs index cf12ed4d..5ed6898c 100644 --- a/src/LibHac/Fs/NcaUtils/Nca.cs +++ b/src/LibHac/Fs/NcaUtils/Nca.cs @@ -148,7 +148,15 @@ namespace LibHac.Fs.NcaUtils // ReSharper disable UnusedParameter.Local private IStorage OpenAesXtsStorage(IStorage baseStorage, int index) { - throw new NotImplementedException("NCA sections using XTS are not supported yet."); + const int sectorSize = 0x200; + + NcaFsHeader fsHeader = Header.GetFsHeader(index); + + byte[] key0 = GetContentKey(NcaKeyType.AesXts0); + byte[] key1 = GetContentKey(NcaKeyType.AesXts1); + + // todo: Handle xts for nca version 3 + return new CachedStorage(new Aes128XtsStorage(baseStorage, key0, key1, sectorSize, true), 2, true); } // ReSharper restore UnusedParameter.Local diff --git a/src/LibHac/SwitchFs.cs b/src/LibHac/SwitchFs.cs index 77e2aada..52470bbc 100644 --- a/src/LibHac/SwitchFs.cs +++ b/src/LibHac/SwitchFs.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; +using System.Linq.Expressions; using LibHac.Fs; using LibHac.Fs.NcaUtils; using LibHac.Fs.Save; @@ -133,42 +134,49 @@ namespace LibHac { foreach (SwitchFsNca nca in Ncas.Values.Where(x => x.Nca.Header.ContentType == ContentType.Meta)) { - var title = new Title(); - - IFileSystem fs = nca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid); - string cnmtPath = fs.EnumerateEntries("*.cnmt").Single().FullPath; - - IFile file = fs.OpenFile(cnmtPath, OpenMode.Read); - - var metadata = new Cnmt(file.AsStream()); - title.Id = metadata.TitleId; - title.Version = metadata.TitleVersion; - title.Metadata = metadata; - title.MetaNca = nca; - title.Ncas.Add(nca); - - foreach (CnmtContentEntry content in metadata.ContentEntries) + try { - string ncaId = content.NcaId.ToHexString(); + var title = new Title(); - if (Ncas.TryGetValue(ncaId, out SwitchFsNca contentNca)) + IFileSystem fs = nca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid); + string cnmtPath = fs.EnumerateEntries("*.cnmt").Single().FullPath; + + IFile file = fs.OpenFile(cnmtPath, OpenMode.Read); + + var metadata = new Cnmt(file.AsStream()); + title.Id = metadata.TitleId; + title.Version = metadata.TitleVersion; + title.Metadata = metadata; + title.MetaNca = nca; + title.Ncas.Add(nca); + + foreach (CnmtContentEntry content in metadata.ContentEntries) { - title.Ncas.Add(contentNca); + string ncaId = content.NcaId.ToHexString(); + + if (Ncas.TryGetValue(ncaId, out SwitchFsNca contentNca)) + { + title.Ncas.Add(contentNca); + } + + switch (content.Type) + { + case CnmtContentType.Program: + case CnmtContentType.Data: + title.MainNca = contentNca; + break; + case CnmtContentType.Control: + title.ControlNca = contentNca; + break; + } } - switch (content.Type) - { - case CnmtContentType.Program: - case CnmtContentType.Data: - title.MainNca = contentNca; - break; - case CnmtContentType.Control: - title.ControlNca = contentNca; - break; - } + Titles[title.Id] = title; + } + catch (Exception ex) + { + Console.WriteLine($"{ex.Message} File: {nca.Filename}"); } - - Titles[title.Id] = title; } }