Support xts nca. Handle more errors in switchfs

This commit is contained in:
Alex Barney 2019-06-22 17:18:28 -05:00
parent abad1ee666
commit 46d4274686
2 changed files with 47 additions and 31 deletions

View file

@ -148,7 +148,15 @@ namespace LibHac.Fs.NcaUtils
// ReSharper disable UnusedParameter.Local // ReSharper disable UnusedParameter.Local
private IStorage OpenAesXtsStorage(IStorage baseStorage, int index) 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 // ReSharper restore UnusedParameter.Local

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Linq.Expressions;
using LibHac.Fs; using LibHac.Fs;
using LibHac.Fs.NcaUtils; using LibHac.Fs.NcaUtils;
using LibHac.Fs.Save; using LibHac.Fs.Save;
@ -133,42 +134,49 @@ namespace LibHac
{ {
foreach (SwitchFsNca nca in Ncas.Values.Where(x => x.Nca.Header.ContentType == ContentType.Meta)) foreach (SwitchFsNca nca in Ncas.Values.Where(x => x.Nca.Header.ContentType == ContentType.Meta))
{ {
var title = new Title(); try
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)
{ {
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) Titles[title.Id] = title;
{ }
case CnmtContentType.Program: catch (Exception ex)
case CnmtContentType.Data: {
title.MainNca = contentNca; Console.WriteLine($"{ex.Message} File: {nca.Filename}");
break;
case CnmtContentType.Control:
title.ControlNca = contentNca;
break;
}
} }
Titles[title.Id] = title;
} }
} }