LibHac/LibHac.Nand/Nand.cs

77 lines
3.1 KiB
C#
Raw Normal View History

2018-07-10 01:34:46 +02:00
using System.IO;
2018-07-09 18:49:59 +02:00
using System.Linq;
using DiscUtils;
using DiscUtils.Fat;
using DiscUtils.Partitions;
using DiscUtils.Streams;
using LibHac.IO;
2018-07-09 18:49:59 +02:00
2018-08-31 17:47:11 +02:00
namespace LibHac.Nand
2018-07-09 18:49:59 +02:00
{
public class Nand
{
2018-07-10 01:34:46 +02:00
private GuidPartitionInfo ProdInfo { get; }
private GuidPartitionInfo ProdInfoF { get; }
private GuidPartitionInfo Safe { get; }
private GuidPartitionInfo System { get; }
private GuidPartitionInfo User { get; }
2018-07-13 17:25:39 +02:00
public Keyset Keyset { get; }
2018-07-10 01:34:46 +02:00
public Nand(Stream stream, Keyset keyset)
2018-07-09 18:49:59 +02:00
{
var disc = new GuidPartitionTable(stream, Geometry.Null);
2018-10-03 00:25:58 +02:00
GuidPartitionInfo[] partitions = disc.Partitions.Select(x => (GuidPartitionInfo)x).ToArray();
2018-07-10 01:34:46 +02:00
ProdInfo = partitions.FirstOrDefault(x => x.Name == "PRODINFO");
ProdInfoF = partitions.FirstOrDefault(x => x.Name == "PRODINFOF");
Safe = partitions.FirstOrDefault(x => x.Name == "SAFE");
System = partitions.FirstOrDefault(x => x.Name == "SYSTEM");
User = partitions.FirstOrDefault(x => x.Name == "USER");
Keyset = keyset;
}
2018-07-09 18:49:59 +02:00
2018-07-10 01:34:46 +02:00
public Stream OpenProdInfo()
{
IStorage encStorage = ProdInfo.Open().AsStorage();
var decStorage = new CachedStorage(new Aes128XtsStorage(encStorage, Keyset.BisKeys[0], 0x4000, true), 0x4000, 4, true);
decStorage.SetReadOnly();
return decStorage.AsStream();
2018-07-10 01:34:46 +02:00
}
2018-07-09 18:49:59 +02:00
public NandPartition OpenProdInfoF()
2018-07-10 01:34:46 +02:00
{
IStorage encStorage = ProdInfoF.Open().AsStorage();
var decStorage = new CachedStorage(new Aes128XtsStorage(encStorage, Keyset.BisKeys[0], 0x4000, true), 0x4000, 4, true);
decStorage.SetReadOnly();
var fat = new FatFileSystem(decStorage.AsStream(), Ownership.None);
return new NandPartition(fat);
2018-07-10 01:34:46 +02:00
}
2018-07-09 18:49:59 +02:00
public NandPartition OpenSafePartition()
2018-07-10 01:34:46 +02:00
{
IStorage encStorage = Safe.Open().AsStorage();
var decStorage = new CachedStorage(new Aes128XtsStorage(encStorage, Keyset.BisKeys[1], 0x4000, true), 0x4000, 4, true);
decStorage.SetReadOnly();
var fat = new FatFileSystem(decStorage.AsStream(), Ownership.None);
return new NandPartition(fat);
2018-07-10 01:34:46 +02:00
}
2018-07-09 18:49:59 +02:00
public NandPartition OpenSystemPartition()
2018-07-10 01:34:46 +02:00
{
IStorage encStorage = System.Open().AsStorage();
var decStorage = new CachedStorage(new Aes128XtsStorage(encStorage, Keyset.BisKeys[2], 0x4000, true), 0x4000, 4, true);
decStorage.SetReadOnly();
var fat = new FatFileSystem(decStorage.AsStream(), Ownership.None);
return new NandPartition(fat);
2018-07-10 01:34:46 +02:00
}
2018-07-09 18:49:59 +02:00
public NandPartition OpenUserPartition()
2018-07-10 01:34:46 +02:00
{
IStorage encStorage = User.Open().AsStorage();
var decStorage = new CachedStorage(new Aes128XtsStorage(encStorage, Keyset.BisKeys[3], 0x4000, true), 0x4000, 4, true);
decStorage.SetReadOnly();
var fat = new FatFileSystem(decStorage.AsStream(), Ownership.None);
return new NandPartition(fat);
2018-07-09 18:49:59 +02:00
}
}
}