LibHac/NandReader/Program.cs

152 lines
5.4 KiB
C#
Raw Normal View History

2018-08-31 18:19:14 +02:00
// ReSharper disable UnusedVariable UnusedMember.Local
using System;
2018-07-10 01:34:46 +02:00
using System.Collections.Generic;
2018-07-09 18:49:59 +02:00
using System.IO;
2018-08-31 17:47:11 +02:00
using LibHac;
using LibHac.Nand;
using LibHac.Savefile;
2018-07-09 18:49:59 +02:00
namespace NandReader
{
public static class Program
{
public static void Main(string[] args)
{
2018-07-10 01:34:46 +02:00
if (args.Length != 1)
{
Console.WriteLine("Usage: NandReader raw_nand_dump_file");
return;
}
2018-07-13 17:25:39 +02:00
GetTitleKeys(args[0]);
}
private static void GetTitleKeys(string nandFile)
{
using (var logger = new ProgressBar())
using (var stream = new FileStream(nandFile, FileMode.Open, FileAccess.Read))
{
2018-10-03 00:25:58 +02:00
Keyset keyset = OpenKeyset();
2018-07-13 17:25:39 +02:00
var nand = new Nand(stream, keyset);
2018-10-03 00:25:58 +02:00
Stream prodinfo = nand.OpenProdInfo();
2018-07-13 17:25:39 +02:00
var calibration = new Calibration(prodinfo);
2018-09-13 03:27:46 +02:00
keyset.EticketExtKeyRsa = Crypto.DecryptRsaKey(calibration.EticketExtKeyRsa, keyset.EticketRsaKek);
2018-10-03 00:25:58 +02:00
Ticket[] tickets = GetTickets(keyset, nand, logger);
2018-07-13 17:25:39 +02:00
2018-10-03 00:25:58 +02:00
foreach (Ticket ticket in tickets)
2018-07-13 17:25:39 +02:00
{
2018-10-03 00:25:58 +02:00
byte[] key = ticket.GetTitleKey(keyset);
2018-07-13 17:25:39 +02:00
logger.LogMessage($"{ticket.RightsId.ToHexString()},{key.ToHexString()}");
}
}
}
private static void ReadSwitchFs(string nandFile)
{
using (var logger = new ProgressBar())
using (var stream = new FileStream(nandFile, FileMode.Open, FileAccess.Read))
{
2018-10-03 00:25:58 +02:00
Keyset keyset = OpenKeyset();
var nand = new Nand(stream, keyset);
2018-10-03 00:25:58 +02:00
NandPartition user = nand.OpenSystemPartition();
2018-08-31 18:07:41 +02:00
var sdfs = new SwitchFs(keyset, user);
}
}
private static void ReadCalibration(string nandFile)
{
using (var logger = new ProgressBar())
using (var stream = new FileStream(nandFile, FileMode.Open, FileAccess.Read))
{
2018-10-03 00:25:58 +02:00
Keyset keyset = OpenKeyset();
var nand = new Nand(stream, keyset);
2018-10-03 00:25:58 +02:00
Stream prodinfo = nand.OpenProdInfo();
var calibration = new Calibration(prodinfo);
}
2018-07-09 18:49:59 +02:00
}
2018-07-10 01:34:46 +02:00
private static void DumpTickets(string nandFile)
2018-07-09 18:49:59 +02:00
{
using (var logger = new ProgressBar())
2018-07-10 01:34:46 +02:00
using (var stream = new FileStream(nandFile, FileMode.Open, FileAccess.Read))
2018-07-09 18:49:59 +02:00
{
2018-10-03 00:25:58 +02:00
Keyset keyset = OpenKeyset();
2018-07-10 01:34:46 +02:00
var nand = new Nand(stream, keyset);
2018-10-03 00:25:58 +02:00
Ticket[] tickets = GetTickets(keyset, nand, logger);
2018-07-10 01:34:46 +02:00
Directory.CreateDirectory("tickets");
2018-10-03 00:25:58 +02:00
foreach (Ticket ticket in tickets)
2018-07-10 01:34:46 +02:00
{
2018-10-03 00:25:58 +02:00
string filename = Path.Combine("tickets", $"{ticket.RightsId.ToHexString()}.tik");
2018-07-10 01:34:46 +02:00
File.WriteAllBytes(filename, ticket.File);
}
2018-07-09 18:49:59 +02:00
}
}
2018-09-16 05:10:42 +02:00
private static Ticket[] GetTickets(Keyset keyset, Nand nand, IProgressReport logger = null)
2018-07-13 17:25:39 +02:00
{
var tickets = new List<Ticket>();
2018-10-03 00:25:58 +02:00
NandPartition system = nand.OpenSystemPartition();
2018-07-13 17:25:39 +02:00
2018-10-03 00:25:58 +02:00
Stream saveE1File = system.OpenFile("save\\80000000000000E1", FileMode.Open, FileAccess.Read);
2018-09-16 05:10:42 +02:00
tickets.AddRange(ReadTickets(keyset, saveE1File));
2018-07-13 17:25:39 +02:00
2018-10-03 00:25:58 +02:00
Stream saveE2 = system.OpenFile("save\\80000000000000E2", FileMode.Open, FileAccess.Read);
2018-09-16 05:10:42 +02:00
tickets.AddRange(ReadTickets(keyset, saveE2));
2018-07-13 17:25:39 +02:00
logger?.LogMessage($"Found {tickets.Count} tickets");
return tickets.ToArray();
}
2018-09-16 05:10:42 +02:00
private static List<Ticket> ReadTickets(Keyset keyset, Stream savefile)
{
var tickets = new List<Ticket>();
var save = new Savefile(keyset, savefile, IntegrityCheckLevel.None);
var ticketList = new BinaryReader(save.OpenFile("/ticket_list.bin"));
var ticketFile = new BinaryReader(save.OpenFile("/ticket.bin"));
2018-10-03 00:25:58 +02:00
ulong titleId = ticketList.ReadUInt64();
while (titleId != ulong.MaxValue)
{
ticketList.BaseStream.Position += 0x18;
2018-10-03 00:25:58 +02:00
long start = ticketFile.BaseStream.Position;
tickets.Add(new Ticket(ticketFile));
ticketFile.BaseStream.Position = start + 0x400;
titleId = ticketList.ReadUInt64();
}
return tickets;
}
2018-07-09 18:49:59 +02:00
private static Keyset OpenKeyset()
{
2018-10-03 00:25:58 +02:00
string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string homeKeyFile = Path.Combine(home, ".switch", "prod.keys");
string homeTitleKeyFile = Path.Combine(home, ".switch", "title.keys");
string homeConsoleKeyFile = Path.Combine(home, ".switch", "console.keys");
2018-07-09 18:49:59 +02:00
string keyFile = null;
string titleKeyFile = null;
string consoleKeyFile = null;
if (File.Exists(homeKeyFile))
{
keyFile = homeKeyFile;
}
if (File.Exists(homeTitleKeyFile))
{
titleKeyFile = homeTitleKeyFile;
}
if (File.Exists(homeConsoleKeyFile))
{
consoleKeyFile = homeConsoleKeyFile;
}
return ExternalKeys.ReadKeyFile(keyFile, titleKeyFile, consoleKeyFile);
}
}
}
2018-07-13 17:25:39 +02:00