Skip to content

Commit

Permalink
Make 3DS code more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
mnadareski committed Aug 8, 2024
1 parent 0aa9463 commit 344d69c
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 208 deletions.
81 changes: 50 additions & 31 deletions NDecrypt.N3DS/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ public static bool IsCodeBinary(this ExeFSFileHeader? header)

#region NCCHHeader

/// <summary>
/// Get the initial value for the plain counter
/// </summary>
public static byte[] PlainIV(this Cart cart, int partitionIndex)
{
if (cart.Partitions == null)
return [];
if (partitionIndex < 0 || partitionIndex >= cart.Partitions.Length)
return [];

var header = cart.Partitions[partitionIndex];
return PlainIV(header);
}

/// <summary>
/// Get the initial value for the plain counter
/// </summary>
Expand All @@ -38,41 +52,54 @@ public static byte[] PlainIV(this NCCHHeader? header)
/// <summary>
/// Get the initial value for the ExeFS counter
/// </summary>
public static byte[] ExeFSIV(this NCCHHeader header)
public static byte[] ExeFSIV(this Cart cart, int partitionIndex)
{
if (header == null)
if (cart.Partitions == null)
return [];
if (partitionIndex < 0 || partitionIndex >= cart.Partitions.Length)
return [];

byte[] partitionIdBytes = BitConverter.GetBytes(header.PartitionId);
return [.. partitionIdBytes, .. Constants.ExefsCounter];
var header = cart.Partitions[partitionIndex];
return ExeFSIV(header);
}

/// <summary>
/// Get the initial value for the RomFS counter
/// Get the initial value for the ExeFS counter
/// </summary>
public static byte[] RomFSIV(this NCCHHeader header)
public static byte[] ExeFSIV(this NCCHHeader? header)
{
if (header == null)
return [];

byte[] partitionIdBytes = BitConverter.GetBytes(header.PartitionId);
return [.. partitionIdBytes, .. Constants.RomfsCounter];
return [.. partitionIdBytes, .. Constants.ExefsCounter];
}

/// <summary>
/// NCCH boot rom key
/// Get the initial value for the RomFS counter
/// </summary>
public static BigInteger KeyX2C { get; set; }
public static byte[] RomFSIV(this Cart cart, int partitionIndex)
{
if (cart.Partitions == null)
return [];
if (partitionIndex < 0 || partitionIndex >= cart.Partitions.Length)
return [];

/// <summary>
/// Normal AES key
/// </summary>
public static BigInteger NormalKey { get; set; }
var header = cart.Partitions[partitionIndex];
return RomFSIV(header);
}

/// <summary>
/// NCCH AES key
/// Get the initial value for the RomFS counter
/// </summary>
public static BigInteger NormalKey2C { get; set; }
public static byte[] RomFSIV(this NCCHHeader? header)
{
if (header == null)
return [];

byte[] partitionIdBytes = BitConverter.GetBytes(header.PartitionId);
return [.. partitionIdBytes, .. Constants.RomfsCounter];
}

#endregion

Expand Down Expand Up @@ -193,6 +220,14 @@ public static MediaTypeIndex MediaTypeIndex(this NCSDHeader? header)
return (MediaTypeIndex)header.PartitionFlags[(int)NCSDFlags.MediaTypeIndex];
}

/// <summary>
/// Media Unit Size i.e. u32 MediaUnitSize = 0x200*2^flags[6];
/// </summary>
public static uint MediaUnitSize(this Cart cart)
{
return cart.Header.MediaUnitSize();
}

/// <summary>
/// Media Unit Size i.e. u32 MediaUnitSize = 0x200*2^flags[6];
/// </summary>
Expand All @@ -217,22 +252,6 @@ public static MediaCardDeviceType MediaCardDevice2X(this NCSDHeader? header)

#endregion

#region PartitionTableEntry

/// <summary>
/// Check for a valid partition
/// </summary>
/// <returns>True if the offset and length are not 0, false otherwise</returns>
public static bool IsValid(this PartitionTableEntry? entry)
{
if (entry == null)
return false;

return entry.Offset != 0 && entry.Length != 0;
}

#endregion

#region Ticket

/// <summary>
Expand Down
15 changes: 15 additions & 0 deletions NDecrypt.N3DS/Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ internal static class Serializer
}
}

cart.Partitions = new NCCHHeader[8];
for (int i = 0; i < 8; i++)
{
// Check the entry is valid
var tableEntry = cart.Header.PartitionsTable![i];
if (tableEntry == null || tableEntry.Offset == 0 || tableEntry.Length == 0)
continue;

// Seek to the beginning of the NCCH partition
long offset = tableEntry.Offset * cart.Header.ImageSizeInMediaUnits;
reader.BaseStream.Seek(offset, SeekOrigin.Begin);

cart.Partitions[i] = ReadNCCHHeader(reader, readSignature: true);
}

return cart;
}
catch
Expand Down
Loading

0 comments on commit 344d69c

Please sign in to comment.