Initial commit

This commit is contained in:
Gericom 2024-01-01 16:09:36 +01:00
commit 0dcb052966
42 changed files with 3003 additions and 0 deletions

63
.gitattributes vendored Normal file
View File

@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto
###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp
###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary
###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary
###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain

261
.gitignore vendored Normal file
View File

@ -0,0 +1,261 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
# Visual Studio 2015 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUNIT
*.VisualState.xml
TestResult.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# DNX
project.lock.json
project.fragment.lock.json
artifacts/
*_i.c
*_p.c
*_i.h
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# JustCode is a .NET coding add-in
.JustCode
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# TODO: Comment the next line if you want to checkin your web deploy settings
# but database connection strings (with potential passwords) will be unencrypted
#*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/packages/*
# except build/, which is used as an MSBuild target.
!**/packages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config
# NuGet v3's project.json files produces more ignoreable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
node_modules/
orleans.codegen.cs
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
# SQL Server files
*.mdf
*.ldf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# JetBrains Rider
.idea/
*.sln.iml
# CodeRush
.cr/
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc

25
GCDTool.sln Normal file
View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34309.116
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GCDTool", "GCDTool\GCDTool.csproj", "{C9C84AF7-5F36-4623-AE76-19AC55B64450}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C9C84AF7-5F36-4623-AE76-19AC55B64450}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C9C84AF7-5F36-4623-AE76-19AC55B64450}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C9C84AF7-5F36-4623-AE76-19AC55B64450}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C9C84AF7-5F36-4623-AE76-19AC55B64450}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {76A73BFB-4C65-473A-821E-CDCA3B40E343}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,40 @@
using System.Buffers.Binary;
namespace GCDTool;
/// <summary>
/// Static class implementing the DSi AES key scrambler.
/// </summary>
public static class AesKeyScrambler
{
private static readonly UInt128 scramblerMagic = new(0xFFFEFB4E29590258ul, 0x2A680F5F1A4F3E79ul);
private const int SCRAMBLER_ROL = 42;
/// <summary>
/// Scrambles the given <paramref name="keyX"/> and <paramref name="keyY"/>.
/// </summary>
/// <param name="keyX">Input key X.</param>
/// <param name="keyY">Input key Y.</param>
/// <returns>The scrambled key.</returns>
public static byte[] Scramble(ReadOnlySpan<byte> keyX, ReadOnlySpan<byte> keyY)
{
UInt128 scrambledKey = Scramble(
BinaryPrimitives.ReadUInt128LittleEndian(keyX),
BinaryPrimitives.ReadUInt128LittleEndian(keyY));
var result = new byte[16];
BinaryPrimitives.WriteUInt128LittleEndian(result, scrambledKey);
return result;
}
/// <summary>
/// Scrambles the given <paramref name="keyX"/> and <paramref name="keyY"/>.
/// </summary>
/// <param name="keyX">Input key X.</param>
/// <param name="keyY">Input key Y.</param>
/// <returns>The scrambled key.</returns>
public static UInt128 Scramble(UInt128 keyX, UInt128 keyY)
{
return UInt128.RotateLeft((keyX ^ keyY) + scramblerMagic, SCRAMBLER_ROL);
}
}

190
GCDTool/Blowfish.cs Normal file
View File

@ -0,0 +1,190 @@
using GCDTool.IO;
namespace GCDTool;
/// <summary>
/// Class for performing Blowfish encryption and decryption.
/// </summary>
public sealed class Blowfish
{
public const int BLOCK_LENGTH = 8;
public const int KEY_TABLE_LENGTH = 0x1048;
public const int P_TABLE_ENTRY_COUNT = 18;
public const int S_BOX_COUNT = 4;
public const int S_BOX_ENTRY_COUNT = 256;
private const string DATA_LENGTH_NOT_MULTIPLE_OF_8_EXCEPTION_MESSAGE = "Data length must be a multiple of 8.";
private const string DESTINATION_BUFFER_TOO_SMALL_EXCEPTION_MESSAGE = "Not enough space in destination buffer.";
private readonly uint[] _pTable;
private readonly uint[][] _sBoxes;
public Blowfish(uint[] pTable, uint[][] sBoxes)
{
if (pTable.Length != P_TABLE_ENTRY_COUNT)
{
throw new ArgumentException($"Size of p table should be {P_TABLE_ENTRY_COUNT}", nameof(pTable));
}
if (sBoxes.Length != S_BOX_COUNT)
{
throw new ArgumentException($"Number of s boxes should be {S_BOX_COUNT}", nameof(sBoxes));
}
for (int i = 0; i < S_BOX_COUNT; i++)
{
if (sBoxes[i].Length != S_BOX_ENTRY_COUNT)
{
throw new ArgumentException($"Size of s box {i} should be {S_BOX_ENTRY_COUNT}", nameof(sBoxes));
}
}
_pTable = pTable;
_sBoxes = sBoxes;
}
public Blowfish(ReadOnlySpan<byte> keyTable)
{
if (keyTable.Length < KEY_TABLE_LENGTH)
{
throw new ArgumentException(nameof(keyTable));
}
_pTable = IOUtil.ReadU32Le(keyTable, P_TABLE_ENTRY_COUNT);
_sBoxes = new uint[S_BOX_COUNT][];
_sBoxes[0] = IOUtil.ReadU32Le(keyTable[0x48..], S_BOX_ENTRY_COUNT);
_sBoxes[1] = IOUtil.ReadU32Le(keyTable[0x448..], S_BOX_ENTRY_COUNT);
_sBoxes[2] = IOUtil.ReadU32Le(keyTable[0x848..], S_BOX_ENTRY_COUNT);
_sBoxes[3] = IOUtil.ReadU32Le(keyTable[0xC48..], S_BOX_ENTRY_COUNT);
}
/// <summary>
/// Encrypts <paramref name="length"/> bytes of data in the given <paramref name="data"/> buffer
/// starting at <paramref name="offset"/>. <paramref name="length"/> must be a multiple of 8.
/// </summary>
/// <param name="data">The data buffer.</param>
/// <param name="offset">The offset in the buffer to start encrypting.</param>
/// <param name="length">The number of bytes to encrypt. Should be a multiple of 8.</param>
public void Encrypt(byte[] data, int offset, int length)
{
Encrypt(data.AsSpan(offset, length));
}
/// <summary>
/// Encrypts the data in the given <paramref name="data"/> span in place.
/// The length of the span must be a multiple of 8.
/// </summary>
/// <param name="data">The data to encrypt.</param>
public void Encrypt(Span<byte> data)
{
ThrowIfDataLengthNotMultipleOf8(data.Length, nameof(data));
for (int i = 0; i < data.Length; i += BLOCK_LENGTH)
{
ulong val = Encrypt(IOUtil.ReadU64Le(data[i..]));
IOUtil.WriteU64Le(data[i..], val);
}
}
/// <summary>
/// Encrypts a single 64 bit value.
/// </summary>
/// <param name="val">The value to encrypt.</param>
/// <returns>The encrypted value.</returns>
public ulong Encrypt(ulong val)
{
uint y = (uint)(val & 0xFFFFFFFF);
uint x = (uint)(val >> 32);
for (int i = 0; i < 16; i++)
{
uint z = _pTable[i] ^ x;
uint a = _sBoxes[0][z >> 24 & 0xFF];
uint b = _sBoxes[1][z >> 16 & 0xFF];
uint c = _sBoxes[2][z >> 8 & 0xFF];
uint d = _sBoxes[3][z & 0xFF];
x = d + (c ^ b + a) ^ y;
y = z;
}
return x ^ _pTable[16] | (ulong)(y ^ _pTable[17]) << 32;
}
/// <summary>
/// Decrypts <paramref name="length"/> bytes of data starting as <paramref name="srcOffset"/> in <paramref name="src"/>
/// and writes it to <paramref name="dst"/> starting from <paramref name="dstOffset"/>.
/// <paramref name="length"/> must be a multiple of 8.
/// </summary>
/// <param name="src">The buffer containing the data to decrypt.</param>
/// <param name="srcOffset">The offset in <paramref name="src"/> where the encrypted data starts.</param>
/// <param name="length">The amount of data to decrypt. Should be a multiple of 8</param>
/// <param name="dst">The buffer to write the decrypted data to.</param>
/// <param name="dstOffset">The offset in <paramref name="dst"/> where the decrypted data should be written.</param>
public void Decrypt(byte[] src, int srcOffset, int length, byte[] dst, int dstOffset)
{
Decrypt(src.AsSpan(srcOffset, length), dst.AsSpan(dstOffset, length));
}
/// <summary>
/// Decrypts the data in the given <paramref name="data"/> span in place.
/// The length of the span must be a multiple of 8.
/// </summary>
/// <param name="data">The data to decrypt.</param>
public void Decrypt(Span<byte> data)
{
Decrypt(data, data);
}
/// <summary>
/// Decrypts the data in the given <paramref name="src"/> span and writes it to the <paramref name="dst"/> span.
/// The length of the <paramref name="src"/> span must be a multiple of 8.
/// <paramref name="dst"/> must have a length equal to or larger than <paramref name="src"/>.
/// </summary>
/// <param name="src">The data to decrypt.</param>
/// <param name="dst">The span to write the decrypted data to.</param>
public void Decrypt(ReadOnlySpan<byte> src, Span<byte> dst)
{
ThrowIfDataLengthNotMultipleOf8(src.Length, nameof(src));
if (dst.Length < src.Length)
{
throw new ArgumentException(DESTINATION_BUFFER_TOO_SMALL_EXCEPTION_MESSAGE, nameof(dst));
}
for (int i = 0; i < src.Length; i += BLOCK_LENGTH)
{
ulong val = Decrypt(IOUtil.ReadU64Le(src[i..]));
IOUtil.WriteU64Le(dst[i..], val);
}
}
/// <summary>
/// Decrypts a single 64 bit value.
/// </summary>
/// <param name="val">The value to decrypt.</param>
/// <returns>The decrypted value.</returns>
public ulong Decrypt(ulong val)
{
uint y = (uint)(val & 0xFFFFFFFF);
uint x = (uint)(val >> 32);
for (int i = 17; i >= 2; i--)
{
uint z = _pTable[i] ^ x;
uint a = _sBoxes[0][z >> 24 & 0xFF];
uint b = _sBoxes[1][z >> 16 & 0xFF];
uint c = _sBoxes[2][z >> 8 & 0xFF];
uint d = _sBoxes[3][z & 0xFF];
x = d + (c ^ b + a) ^ y;
y = z;
}
return x ^ _pTable[1] | (ulong)(y ^ _pTable[0]) << 32;
}
private void ThrowIfDataLengthNotMultipleOf8(int dataLength, string paramName)
{
if ((dataLength & 7) != 0)
{
throw new ArgumentException(DATA_LENGTH_NOT_MULTIPLE_OF_8_EXCEPTION_MESSAGE, paramName);
}
}
}

View File

@ -0,0 +1,30 @@
using CommandLine;
namespace GCDTool;
/// <summary>
/// Class containing the command line options of GCDTool.
/// </summary>
sealed class CommandLineOptions
{
[Option("arm9", Required = true, HelpText = "ARM9 elf file path.")]
public required string Arm9Path { get; init; }
[Option("arm7", Required = true, HelpText = "ARM7 elf file path.")]
public required string Arm7Path { get; init; }
[Option("wram", Required = true, HelpText = "Initial WRAM configuration json file path.")]
public required string WramConfigJsonPath { get; init; }
[Option("rsakey", Required = true, HelpText = "GCD RSA private key der file path.")]
public required string GcdKeyPath { get; init; }
[Option("gamecode", Default = "####", Required = false, HelpText = "The gamecode to use.")]
public required string GameCode { get; init; }
[Option("arm9-67-mhz", Default = false, Required = false, HelpText = "When true, the ARM9 will start at 67 MHz instead of 134 MHz.")]
public bool Arm9Speed67MHz { get; init; }
[Value(0, MetaName = "output path", Required = true, HelpText = "The path of the gcd file to create.")]
public required string OutputPath { get; init; }
}

51
GCDTool/Elf/ElfFile.cs Normal file
View File

@ -0,0 +1,51 @@
using GCDTool.Elf.Sections;
using GCDTool.IO;
namespace GCDTool.Elf;
sealed class ElfFile
{
public ElfFile(byte[] data)
{
var er = new EndianBinaryReader(new MemoryStream(data), Endianness.LittleEndian);
Header = new ElfHeader(er);
er.BaseStream.Position = Header.ProgramHeaderTableOffset;
ProgramHeaderTable = new ProgramHeaderTableEntry[Header.ProgramHeaderTableEntryCount];
for (int i = 0; i < Header.ProgramHeaderTableEntryCount; i++)
{
ProgramHeaderTable[i] = new ProgramHeaderTableEntry(er);
}
er.BaseStream.Position = Header.SectionHeaderTableOffset;
SectionHeaderTable = new SectionHeaderTableEntry[Header.SectionHeaderTableEntryCount];
for (int i = 0; i < Header.SectionHeaderTableEntryCount; i++)
{
SectionHeaderTable[i] = new SectionHeaderTableEntry(er);
}
er.Close();
var namestab = new ElfStrtab(SectionHeaderTable[Header.SectionNamesIndex], string.Empty);
Sections = new ElfSection[SectionHeaderTable.Length];
var sectionFactory = new ElfSectionFactory();
for (int i = 0; i < SectionHeaderTable.Length; i++)
{
Sections[i] = sectionFactory.CreateElfSection(SectionHeaderTable[i],
namestab.GetString(SectionHeaderTable[i].NameOffset));
}
}
public ElfHeader Header;
public ProgramHeaderTableEntry[] ProgramHeaderTable;
public SectionHeaderTableEntry[] SectionHeaderTable;
public ElfSection[] Sections { get; }
public ElfSection? GetSectionByName(string name)
{
return Sections.FirstOrDefault(section => section.Name == name);
}
}

58
GCDTool/Elf/ElfHeader.cs Normal file
View File

@ -0,0 +1,58 @@
using GCDTool.IO;
namespace GCDTool.Elf;
sealed class ElfHeader
{
private const uint ElfHeaderMagic = 0x464C457F;
public ElfHeader(EndianBinaryReader er)
{
Magic = er.Read<uint>();
if (Magic != ElfHeaderMagic)
{
throw new InvalidDataException("Elf magic invalid!");
}
BitFormat = er.Read<byte>();
Endianness = er.Read<byte>();
Version = er.Read<byte>();
Abi = er.Read<byte>();
AbiVersion = er.Read<byte>();
Padding = er.Read<byte>(7);
ObjectType = er.Read<ushort>();
Architecture = er.Read<ushort>();
Version2 = er.Read<uint>();
EntryPoint = er.Read<uint>();
ProgramHeaderTableOffset = er.Read<uint>();
SectionHeaderTableOffset = er.Read<uint>();
Flags = er.Read<uint>();
HeaderSize = er.Read<ushort>();
ProgramHeaderTableEntrySize = er.Read<ushort>();
ProgramHeaderTableEntryCount = er.Read<ushort>();
SectionHeaderTableEntrySize = er.Read<ushort>();
SectionHeaderTableEntryCount = er.Read<ushort>();
SectionNamesIndex = er.Read<ushort>();
}
public uint Magic;
public byte BitFormat;
public byte Endianness;
public byte Version;
public byte Abi;
public byte AbiVersion;
public byte[] Padding;//7
public ushort ObjectType;
public ushort Architecture;
public uint Version2;
public uint EntryPoint;
public uint ProgramHeaderTableOffset;
public uint SectionHeaderTableOffset;
public uint Flags;
public ushort HeaderSize;
public ushort ProgramHeaderTableEntrySize;
public ushort ProgramHeaderTableEntryCount;
public ushort SectionHeaderTableEntrySize;
public ushort SectionHeaderTableEntryCount;
public ushort SectionNamesIndex;
}

View File

@ -0,0 +1,29 @@
namespace GCDTool.Elf;
enum ElfSectionType : uint
{
Null = 0,
Progbits = 1,
Symtab = 2,
Strtab = 3,
Rela = 4,
Hash = 5,
Dynamic = 6,
Note = 7,
Nobits = 8,
Rel = 9,
Shlib = 10,
Dynsym = 11,
InitArray = 14,
FiniArray = 15,
PreinitArray = 16,
Group = 17,
SymtabShndx = 18,
Num = 19,
Loos = 0x60000000,
Hios = 0x6fffffff,
Loproc = 0x70000000,
Hiproc = 0x7fffffff,
Louser = 0x80000000,
Hiuser = 0xffffffff
}

View File

@ -0,0 +1,54 @@
using GCDTool.IO;
namespace GCDTool.Elf;
sealed class ProgramHeaderTableEntry
{
public enum ElfSegmentType : uint
{
Null = 0,
Load = 1,
Dynamic = 2,
Interp = 3,
Note = 4,
Shlib = 5,
Phdr = 6,
Tls = 7,
Num = 8,
Loos = 0x60000000,
Hios = 0x6fffffff,
Loproc = 0x70000000,
Hiproc = 0x7fffffff
}
public ProgramHeaderTableEntry(EndianBinaryReader er)
{
SegmentType = er.Read<ElfSegmentType>();
FileImageOffset = er.Read<uint>();
VirtualAddress = er.Read<uint>();
PhysicalAddress = er.Read<uint>();
FileImageSize = er.Read<uint>();
MemorySize = er.Read<uint>();
Flags = er.Read<uint>();
Alignment = er.Read<uint>();
if (FileImageSize != 0)
{
long curpos = er.BaseStream.Position;
er.BaseStream.Position = FileImageOffset;
SegmentData = er.Read<byte>((int)FileImageSize);
er.BaseStream.Position = curpos;
}
}
public ElfSegmentType SegmentType;
public uint FileImageOffset;
public uint VirtualAddress;
public uint PhysicalAddress;
public uint FileImageSize;
public uint MemorySize;
public uint Flags;
public uint Alignment;
public byte[] SegmentData = [];
}

View File

@ -0,0 +1,41 @@
using GCDTool.IO;
namespace GCDTool.Elf;
sealed class SectionHeaderTableEntry
{
public SectionHeaderTableEntry(EndianBinaryReader er)
{
NameOffset = er.Read<uint>();
SectionType = er.Read<ElfSectionType>();
Flags = er.Read<uint>();
VirtualAddress = er.Read<uint>();
FileImageOffset = er.Read<uint>();
FileImageSize = er.Read<uint>();
Link = er.Read<uint>();
Info = er.Read<uint>();
Alignment = er.Read<uint>();
EntrySize = er.Read<uint>();
if (FileImageSize != 0)
{
long curpos = er.BaseStream.Position;
er.BaseStream.Position = FileImageOffset;
SectionData = er.Read<byte>((int)FileImageSize);
er.BaseStream.Position = curpos;
}
}
public uint NameOffset;
public ElfSectionType SectionType;
public uint Flags;
public uint VirtualAddress;
public uint FileImageOffset;
public uint FileImageSize;
public uint Link;
public uint Info;
public uint Alignment;
public uint EntrySize;
public byte[] SectionData = [];
}

View File

@ -0,0 +1,18 @@
namespace GCDTool.Elf.Sections;
class ElfSection
{
public SectionHeaderTableEntry SectionHeader { get; }
public string Name { get; }
public ElfSection(SectionHeaderTableEntry sectionHeader, string name)
{
SectionHeader = sectionHeader;
Name = name;
}
public override string ToString()
{
return Name;
}
}

View File

@ -0,0 +1,14 @@
namespace GCDTool.Elf.Sections;
sealed class ElfSectionFactory
{
public ElfSection CreateElfSection(SectionHeaderTableEntry section, string name)
{
return section.SectionType switch
{
ElfSectionType.Strtab => new ElfStrtab(section, name),
ElfSectionType.Symtab => new ElfSymtab(section, name),
_ => new ElfSection(section, name),
};
}
}

View File

@ -0,0 +1,22 @@
namespace GCDTool.Elf.Sections;
sealed class ElfStrtab : ElfSection
{
public ElfStrtab(SectionHeaderTableEntry section, string name)
: base(section, name) { }
public string GetString(uint offset)
{
string result = string.Empty;
while (offset < SectionHeader.SectionData.Length)
{
char c = (char)SectionHeader.SectionData[offset++];
if (c == '\0')
{
return result;
}
result += c;
}
return result;
}
}

View File

@ -0,0 +1,23 @@
using GCDTool.IO;
namespace GCDTool.Elf.Sections;
sealed class ElfSymbol
{
public ElfSymbol(EndianBinaryReader er)
{
NameOffset = er.Read<uint>();
Value = er.Read<uint>();
Size = er.Read<uint>();
Info = er.Read<byte>();
Other = er.Read<byte>();
SectionIndex = er.Read<ushort>();
}
public uint NameOffset;
public uint Value;
public uint Size;
public byte Info;
public byte Other;
public ushort SectionIndex;
}

View File

@ -0,0 +1,22 @@
using GCDTool.IO;
namespace GCDTool.Elf.Sections;
sealed class ElfSymtab : ElfSection
{
public IReadOnlyList<ElfSymbol> Symbols { get; }
public ElfSymtab(SectionHeaderTableEntry section, string name)
: base(section, name)
{
uint nrEntries = section.FileImageSize / section.EntrySize;
var symbols = new ElfSymbol[nrEntries];
var er = new EndianBinaryReader(new MemoryStream(section.SectionData), Endianness.LittleEndian);
for (int i = 0; i < nrEntries; i++)
{
symbols[i] = new ElfSymbol(er);
}
Symbols = symbols;
er.Close();
}
}

17
GCDTool/GCDTool.csproj Normal file
View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Authors>Gericom</Authors>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BouncyCastle.Cryptography" Version="2.2.1" />
<PackageReference Include="CommandLineParser" Version="2.9.1" />
</ItemGroup>
</Project>

392
GCDTool/GcdBlowfish.cs Normal file
View File

@ -0,0 +1,392 @@
using GCDTool.IO;
using System.Buffers.Binary;
namespace GCDTool;
/// <summary>
/// Static helper class for the GCD blowfish table.
/// </summary>
static class GcdBlowfish
{
/// <summary>
/// Creates the GCD blowfish table corresponding to the given <paramref name="gameCode"/>.
/// </summary>
/// <param name="gameCode">The game code.</param>
/// <returns>The transformed blowfish table.</returns>
public static byte[] GetTransformedKeyTable(uint gameCode)
{
var keyTable = KeyTable.ToArray();
uint keyCode = BinaryPrimitives.ReverseEndianness(gameCode);
for (int i = 0; i < Blowfish.P_TABLE_ENTRY_COUNT; i++)
{
IOUtil.WriteU32Le(keyTable, i * 4,
IOUtil.ReadU32Le(keyTable, i * 4) ^ keyCode);
}
var scratch = new byte[8];
for (int i = 0; i < Blowfish.KEY_TABLE_LENGTH; i += Blowfish.BLOCK_LENGTH)
{
var blowfish = new Blowfish(keyTable);
blowfish.Encrypt(scratch);
Array.Copy(scratch, 4, keyTable, i, 4);
Array.Copy(scratch, 0, keyTable, i + 4, 4);
}
return keyTable;
}
/// <summary>
/// The untransformed GCD blowfish table.
/// </summary>
public static ReadOnlySpan<byte> KeyTable =>
[
0xEE, 0xA8, 0x95, 0x75, 0x2D, 0xF3, 0xFF, 0x84, 0xC6, 0xAE, 0xF8, 0x58,
0xC2, 0x44, 0x64, 0x6F, 0xBC, 0xCF, 0xA6, 0x10, 0x13, 0xB8, 0xE1, 0xBE,
0xC3, 0xAB, 0xEF, 0x88, 0xCD, 0x26, 0x20, 0xC7, 0x3A, 0x91, 0x0B, 0xC0,
0xC0, 0x74, 0xB0, 0x9F, 0x0F, 0x83, 0xD4, 0x56, 0xE5, 0xDE, 0xAB, 0x69,
0xF2, 0x5F, 0x6E, 0xCF, 0x2F, 0xBE, 0xFE, 0xD7, 0xE2, 0xD5, 0xF5, 0x84,
0xDA, 0xCC, 0xA1, 0x73, 0x99, 0x59, 0x20, 0x44, 0x63, 0x8F, 0x27, 0x74,
0x53, 0x72, 0x90, 0xF0, 0x8F, 0xD4, 0x95, 0x1C, 0x99, 0xCE, 0xDB, 0x7C,
0x8A, 0x50, 0xB9, 0xA8, 0x9E, 0x9F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x23, 0x24, 0x60, 0x55,
0x22, 0xB0, 0xCF, 0x1A, 0x6B, 0xF1, 0xC9, 0x68, 0xA5, 0x28, 0xBC, 0xBE,
0x5E, 0xD1, 0xD1, 0xB1, 0x30, 0x41, 0xF5, 0xA9, 0x93, 0x0B, 0xBB, 0x0E,
0xB9, 0xFD, 0x3F, 0xAF, 0xAD, 0x8A, 0x07, 0x52, 0x60, 0x21, 0xBF, 0xB3,
0x05, 0x9D, 0x4D, 0xFB, 0xA3, 0x85, 0x27, 0xE3, 0xE5, 0x87, 0x5A, 0xD0,
0xA5, 0x68, 0x8E, 0x68, 0x76, 0x03, 0x84, 0x38, 0x8D, 0xAC, 0x46, 0x9B,
0xFD, 0x86, 0xF2, 0x23
];
}

133
GCDTool/GcdHeader.cs Normal file
View File

@ -0,0 +1,133 @@
using GCDTool.IO;
using GCDTool.Wram;
using System.Security.Cryptography;
namespace GCDTool;
/// <summary>
/// Class representing the header of a GCD rom file.
/// </summary>
sealed class GcdHeader
{
/// <summary>
/// The game code of the GCD rom.
/// </summary>
public uint GameCode { get; set; }
/// <summary>
/// The offset of the ARM 9 binary in the rom.
/// </summary>
public uint Arm9RomOffset { get; set; }
/// <summary>
/// The original size of the ARM 9 binary.
/// </summary>
public uint Arm9Size { get; set; }
/// <summary>
/// The memory address where the ARM 9 binary will be loaded.
/// </summary>
public uint Arm9LoadAddress { get; set; }
/// <summary>
/// The padded size of the ARM 9 binary.
/// </summary>
public uint Arm9PaddedSize { get; set; }
/// <summary>
/// The offset of the ARM 7 binary in the rom.
/// </summary>
public uint Arm7RomOffset { get; set; }
/// <summary>
/// The original size of the ARM 7 binary.
/// </summary>
public uint Arm7Size { get; set; }
/// <summary>
/// The memory address where the ARM 7 binary will be loaded.
/// </summary>
public uint Arm7LoadAddress { get; set; }
/// <summary>
/// The padded size of the ARM 7 binary.
/// </summary>
public uint Arm7PaddedSize { get; set; }
/// <summary>
/// The regular rom control settings.
/// </summary>
public uint RomControl { get; set; }
/// <summary>
/// The secure rom control settings.
/// </summary>
public uint RomControlSecure { get; set; }
/// <summary>
/// The secure area delay.
/// </summary>
public ushort SecureAreaDelay { get; set; }
/// <summary>
/// The end of the nitro region of the rom.
/// </summary>
public ushort NitroRomRegionEnd { get; set; }
/// <summary>
/// The start of the twl region of the rom.
/// </summary>
public ushort TwlRomRegionStart { get; set; }
/// <summary>
/// Flags controlling how the GCD firm is loaded.
/// </summary>
public GcdHeaderFlags Flags { get; set; }
/// <summary>
/// The RSA signed signature. See <see cref="GcdSignature"/> for the format before signing.
/// </summary>
public byte[] Signature { get; } = new byte[GcdSignature.SIGNATURE_SIGNED_LENGTH];
/// <summary>
/// The initial WRAM configuration used by the GCD firm.
/// </summary>
public WramConfig WramConfiguration { get; set; } = new();
/// <summary>
/// Serializes the header data as a byte array.
/// </summary>
/// <returns>The header data as a byte array.</returns>
public byte[] ToByteArray()
{
var header = new byte[0x200];
IOUtil.WriteU32Le(header, 0x0C, GameCode);
IOUtil.WriteU32Le(header, 0x20, Arm9RomOffset);
IOUtil.WriteU32Le(header, 0x24, Arm9Size);
IOUtil.WriteU32Le(header, 0x28, Arm9LoadAddress);
IOUtil.WriteU32Le(header, 0x2C, Arm9PaddedSize);
IOUtil.WriteU32Le(header, 0x30, Arm7RomOffset);
IOUtil.WriteU32Le(header, 0x34, Arm7Size);
IOUtil.WriteU32Le(header, 0x38, Arm7LoadAddress);
IOUtil.WriteU32Le(header, 0x3C, Arm7PaddedSize);
IOUtil.WriteU32Le(header, 0x60, RomControl);
IOUtil.WriteU32Le(header, 0x64, RomControlSecure);
IOUtil.WriteU16Le(header, 0x6E, SecureAreaDelay);
IOUtil.WriteU16Le(header, 0x90, NitroRomRegionEnd);
IOUtil.WriteU16Le(header, 0x92, TwlRomRegionStart);
header[0xFF] = (byte)Flags;
Signature.CopyTo(header, 0x100);
WramConfiguration.Write(header.AsSpan(0x180));
return header;
}
/// <summary>
/// Calculates the SHA-1 hash used for <see cref="GcdSignature.HeaderSha1"/>.
/// </summary>
/// <param name="destination">The byte span to write the SHA-1 hash to.</param>
public void GetSha1Hash(Span<byte> destination)
{
var headerData = ToByteArray();
SHA1.HashData([.. headerData.AsSpan(0, 0x100), .. headerData.AsSpan(0x180, 0x80)], destination);
}
}

View File

@ -0,0 +1,9 @@
namespace GCDTool;
[Flags]
enum GcdHeaderFlags : byte
{
Arm9Compressed = (1 << 0),
Arm7Compressed = (1 << 1),
Arm9Speed134MHz = (1 << 2)
}

88
GCDTool/GcdRom.cs Normal file
View File

@ -0,0 +1,88 @@
namespace GCDTool;
/// <summary>
/// Class representing a GCD rom file.
/// </summary>
sealed class GcdRom
{
private const int BLOWFISH_P_TABLE_OFFSET = 0x1600;
private const int BLOWFISH_S_BOXES_OFFSET = 0x1C00;
private const int TEST_PATTERNS_OFFSET = 0x3000;
/// <summary>
/// The header of the GCD rom.
/// </summary>
public GcdHeader Header { get; set; } = new();
/// <summary>
/// The encrypted ARM 9 binary.
/// </summary>
public byte[] EncryptedArm9Binary { get; set; } = [];
/// <summary>
/// The encrypted ARM 7 binary.
/// </summary>
public byte[] EncryptedArm7Binary { get; set; } = [];
/// <summary>
/// Writes the GCD rom file to the given <paramref name="stream"/>.
/// </summary>
/// <param name="stream">The stream to write to.</param>
public void Write(Stream stream)
{
stream.Write(Header.ToByteArray());
WriteBlowfishTable(stream);
WriteTestPatterns(stream);
stream.Position = Header.Arm9RomOffset;
stream.Write(EncryptedArm9Binary);
stream.Position = Header.Arm7RomOffset;
stream.Write(EncryptedArm7Binary);
}
private void WriteBlowfishTable(Stream stream)
{
var blowfishTable = GcdBlowfish.GetTransformedKeyTable(Header.GameCode);
stream.Position = BLOWFISH_P_TABLE_OFFSET;
stream.Write(blowfishTable, 0, Blowfish.P_TABLE_ENTRY_COUNT * 4);
stream.Position = BLOWFISH_S_BOXES_OFFSET;
stream.Write(blowfishTable, Blowfish.P_TABLE_ENTRY_COUNT * 4, Blowfish.S_BOX_COUNT * Blowfish.S_BOX_ENTRY_COUNT * 4);
}
private void WriteTestPatterns(Stream stream)
{
stream.Position = TEST_PATTERNS_OFFSET;
stream.Write([0xFF, 0x00, 0xFF, 0x00, 0xAA, 0x55, 0xAA, 0x55]);
for (int i = 8; i < 0x200; i++)
{
stream.WriteByte((byte)(i & 0xFF));
}
for (int i = 0; i < 0x200; i++)
{
stream.WriteByte((byte)(0xFF - (i & 0xFF)));
}
for (int i = 0; i < 0x200; i++)
{
stream.WriteByte(0x00);
}
for (int i = 0; i < 0x200; i++)
{
stream.WriteByte(0xFF);
}
for (int i = 0; i < 0x200; i++)
{
stream.WriteByte(0x0F);
}
for (int i = 0; i < 0x200; i++)
{
stream.WriteByte(0xF0);
}
for (int i = 0; i < 0x200; i++)
{
stream.WriteByte(0x55);
}
for (int i = 0; i < 0x1FF; i++)
{
stream.WriteByte(0xAA);
}
}
}

76
GCDTool/GcdSignature.cs Normal file
View File

@ -0,0 +1,76 @@
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.OpenSsl;
using System.Security.Cryptography;
namespace GCDTool;
/// <summary>
/// Class representing a GCD signature.
/// </summary>
sealed class GcdSignature
{
/// <summary>
/// The length of the signature data before signing.
/// </summary>
public const int SIGNATURE_LENGTH = 0x74;
/// <summary>
/// The length of the signature data after signing.
/// </summary>
public const int SIGNATURE_SIGNED_LENGTH = 0x80;
/// <summary>
/// AES key Y used for encrypting the ARM 9 and ARM 7 binaries.
/// </summary>
public byte[] AesKeyY { get; } = new byte[16];
/// <summary>
/// The SHA-1 hash of the 0x180 bytes consisting of header[0..0x100] and header[0x180..0x200].
/// </summary>
public byte[] HeaderSha1 { get; } = new byte[20];
/// <summary>
/// The SHA-1 hash of the original ARM 9 binary (without padding and AES encryption).
/// </summary>
public byte[] Arm9Sha1 { get; } = new byte[20];
/// <summary>
/// The SHA-1 hash of the original ARM 7 binary (without padding and AES encryption).
/// </summary>
public byte[] Arm7Sha1 { get; } = new byte[20];
/// <summary>
/// Serializes the signature as a byte array.
/// </summary>
/// <returns>The signature as byte array.</returns>
public byte[] ToByteArray()
{
var signature = new byte[SIGNATURE_LENGTH];
AesKeyY.CopyTo(signature, 0x00);
HeaderSha1.CopyTo(signature, 0x10);
Arm9Sha1.CopyTo(signature, 0x24);
Arm7Sha1.CopyTo(signature, 0x38);
SHA1.HashData(signature.AsSpan(0, 0x60), signature.AsSpan(0x60));
return signature;
}
/// <summary>
/// Serializes the signature as a byte array and signs the result
/// using the RSA private key specified by <paramref name="derKeyPath"/>.
/// </summary>
/// <param name="derKeyPath">The path to the .der file containing the private RSA key to use.</param>
/// <returns>A byte array containing the signed signature.</returns>
public byte[] ToSignedByteArray(string derKeyPath)
{
var rsaCryptoServiceProvider = new RSACryptoServiceProvider();
rsaCryptoServiceProvider.ImportRSAPrivateKey(File.ReadAllBytes(derKeyPath), out _);
string pem = rsaCryptoServiceProvider.ExportRSAPrivateKeyPem();
var keyPair = (AsymmetricCipherKeyPair)new PemReader(new StringReader(pem)).ReadObject();
var rsa = new Pkcs1Encoding(new RsaEngine());
rsa.Init(true, keyPair.Private);
return rsa.ProcessBlock(ToByteArray(), 0, SIGNATURE_LENGTH);
}
}

View File

@ -0,0 +1,152 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Text;
namespace GCDTool.IO;
sealed class EndianBinaryReader : IDisposable
{
private bool _disposed;
private byte[]? _buffer;
public Stream BaseStream { get; }
public Endianness Endianness { get; }
public static Endianness SystemEndianness =>
BitConverter.IsLittleEndian ? Endianness.LittleEndian : Endianness.BigEndian;
private bool Reverse => SystemEndianness != Endianness;
public EndianBinaryReader(Stream baseStream, Endianness endianness = Endianness.LittleEndian)
{
if (!baseStream.CanRead)
{
throw new ArgumentException("Stream is not readable.", nameof(baseStream));
}
BaseStream = baseStream;
Endianness = endianness;
}
~EndianBinaryReader()
{
Dispose(false);
}
[MemberNotNull(nameof(_buffer))]
private void FillBuffer(int bytes, int stride)
{
if (_buffer == null || _buffer.Length < bytes)
{
_buffer = new byte[bytes];
}
BaseStream.Read(_buffer, 0, bytes);
if (Reverse && stride > 1)
{
for (int i = 0; i < bytes; i += stride)
{
Array.Reverse(_buffer, i, stride);
}
}
}
public char ReadChar(Encoding encoding)
{
int size = GetEncodingSize(encoding);
FillBuffer(size, size);
return encoding.GetChars(_buffer, 0, size)[0];
}
public char[] ReadChars(Encoding encoding, int count)
{
int size = GetEncodingSize(encoding);
FillBuffer(size * count, size);
return encoding.GetChars(_buffer, 0, size * count);
}
private static int GetEncodingSize(Encoding encoding)
{
if (encoding == Encoding.UTF8 || encoding == Encoding.ASCII)
{
return 1;
}
else if (encoding == Encoding.Unicode || encoding == Encoding.BigEndianUnicode)
{
return 2;
}
else
{
return 1;
}
}
public string ReadStringNT(Encoding encoding)
{
string text = string.Empty;
do
{
text += ReadChar(encoding);
} while (!text.EndsWith("\0", StringComparison.Ordinal));
return text.Remove(text.Length - 1);
}
public string ReadString(Encoding encoding, int count)
{
return new string(ReadChars(encoding, count));
}
public unsafe T Read<T>() where T : unmanaged
{
int size = sizeof(T);
FillBuffer(size, size);
return MemoryMarshal.Read<T>(_buffer);
}
public unsafe T[] Read<T>(int count) where T : unmanaged
{
int size = sizeof(T);
var result = new T[count];
var byteResult = MemoryMarshal.Cast<T, byte>(result);
BaseStream.Read(byteResult);
if (Reverse && size > 1)
{
for (int i = 0; i < size * count; i += size)
{
byteResult.Slice(i, size).Reverse();
}
}
return result;
}
public void Close()
{
Dispose();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
if (disposing)
{
BaseStream?.Close();
}
_buffer = null;
_disposed = true;
}
}

7
GCDTool/IO/Endianness.cs Normal file
View File

@ -0,0 +1,7 @@
namespace GCDTool.IO;
enum Endianness
{
BigEndian,
LittleEndian
}

221
GCDTool/IO/IOUtil.cs Normal file
View File

@ -0,0 +1,221 @@
using System.Buffers.Binary;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace GCDTool.IO;
static class IOUtil
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static short ReadS16Le(byte[] data, int offset)
=> BinaryPrimitives.ReadInt16LittleEndian(data.AsSpan(offset));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static short ReadS16Le(ReadOnlySpan<byte> span)
=> BinaryPrimitives.ReadInt16LittleEndian(span);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static short[] ReadS16Le(byte[] data, int offset, int count)
=> ReadS16Le(data.AsSpan(offset), count);
public static short[] ReadS16Le(ReadOnlySpan<byte> data, int count)
{
var res = MemoryMarshal.Cast<byte, short>(data.Slice(0, count * 2)).ToArray();
if (!BitConverter.IsLittleEndian)
{
for (int i = 0; i < count; i++)
res[i] = BinaryPrimitives.ReverseEndianness(res[i]);
}
return res;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteS16Le(byte[] data, int offset, short value)
=> BinaryPrimitives.WriteInt16LittleEndian(data.AsSpan(offset), value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteS16Le(Span<byte> span, short value)
=> BinaryPrimitives.WriteInt16LittleEndian(span, value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteS16Le(byte[] data, int offset, ReadOnlySpan<short> values)
=> WriteS16Le(data.AsSpan(offset), values);
public static void WriteS16Le(Span<byte> data, ReadOnlySpan<short> values)
{
var dst = MemoryMarshal.Cast<byte, short>(data);
values.CopyTo(dst);
if (!BitConverter.IsLittleEndian)
{
for (int i = 0; i < values.Length; i++)
dst[i] = BinaryPrimitives.ReverseEndianness(dst[i]);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static short ReadS16Be(byte[] data, int offset)
=> BinaryPrimitives.ReadInt16BigEndian(data.AsSpan(offset));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static short ReadS16Be(ReadOnlySpan<byte> span)
=> BinaryPrimitives.ReadInt16BigEndian(span);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort[] ReadU16Le(byte[] data, int offset, int count)
=> ReadU16Le(data.AsSpan(offset), count);
public static ushort[] ReadU16Le(ReadOnlySpan<byte> data, int count)
{
var res = MemoryMarshal.Cast<byte, ushort>(data.Slice(0, count * 2)).ToArray();
if (!BitConverter.IsLittleEndian)
{
for (int i = 0; i < count; i++)
res[i] = BinaryPrimitives.ReverseEndianness(res[i]);
}
return res;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort ReadU16Le(byte[] data, int offset)
=> BinaryPrimitives.ReadUInt16LittleEndian(data.AsSpan(offset));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort ReadU16Le(ReadOnlySpan<byte> span)
=> BinaryPrimitives.ReadUInt16LittleEndian(span);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteU16Le(byte[] data, int offset, ushort value)
=> BinaryPrimitives.WriteUInt16LittleEndian(data.AsSpan(offset), value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteU16Le(Span<byte> span, ushort value)
=> BinaryPrimitives.WriteUInt16LittleEndian(span, value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteU16Le(byte[] data, int offset, ReadOnlySpan<ushort> values)
=> WriteU16Le(data.AsSpan(offset), values);
public static void WriteU16Le(Span<byte> data, ReadOnlySpan<ushort> values)
{
var dst = MemoryMarshal.Cast<byte, ushort>(data);
values.CopyTo(dst);
if (!BitConverter.IsLittleEndian)
{
for (int i = 0; i < values.Length; i++)
dst[i] = BinaryPrimitives.ReverseEndianness(dst[i]);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort ReadU16Be(byte[] data, int offset)
=> BinaryPrimitives.ReadUInt16BigEndian(data.AsSpan(offset));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort ReadU16Be(ReadOnlySpan<byte> span)
=> BinaryPrimitives.ReadUInt16BigEndian(span);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteU16Be(byte[] data, int offset, ushort value)
=> BinaryPrimitives.WriteUInt16BigEndian(data.AsSpan(offset), value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteU16Be(Span<byte> span, ushort value)
=> BinaryPrimitives.WriteUInt16BigEndian(span, value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint ReadU24Le(byte[] data, int offset)
=> (uint)(data[offset] | data[offset + 1] << 8 | data[offset + 2] << 16);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint ReadU24Le(ReadOnlySpan<byte> span)
=> (uint)(span[0] | span[1] << 8 | span[2] << 16);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint ReadU32Le(byte[] data, int offset)
=> BinaryPrimitives.ReadUInt32LittleEndian(data.AsSpan(offset));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint ReadU32Le(ReadOnlySpan<byte> span)
=> BinaryPrimitives.ReadUInt32LittleEndian(span);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint[] ReadU32Le(byte[] data, int offset, int count)
=> ReadU32Le(data.AsSpan(offset), count);
public static uint[] ReadU32Le(ReadOnlySpan<byte> data, int count)
{
var res = MemoryMarshal.Cast<byte, uint>(data.Slice(0, count * 4)).ToArray();
if (!BitConverter.IsLittleEndian)
{
for (int i = 0; i < count; i++)
res[i] = BinaryPrimitives.ReverseEndianness(res[i]);
}
return res;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint ReadU32Be(byte[] data, int offset)
=> BinaryPrimitives.ReadUInt32BigEndian(data.AsSpan(offset));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint ReadU32Be(ReadOnlySpan<byte> span)
=> BinaryPrimitives.ReadUInt32BigEndian(span);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteU32Le(byte[] data, int offset, uint value)
=> BinaryPrimitives.WriteUInt32LittleEndian(data.AsSpan(offset), value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteU32Le(Span<byte> span, uint value)
=> BinaryPrimitives.WriteUInt32LittleEndian(span, value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteU32Le(byte[] data, int offset, ReadOnlySpan<uint> values)
=> WriteU32Le(data.AsSpan(offset), values);
public static void WriteU32Le(Span<byte> data, ReadOnlySpan<uint> values)
{
var dst = MemoryMarshal.Cast<byte, uint>(data);
values.CopyTo(dst);
if (!BitConverter.IsLittleEndian)
{
for (int i = 0; i < values.Length; i++)
dst[i] = BinaryPrimitives.ReverseEndianness(dst[i]);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong ReadU64Le(byte[] data, int offset)
=> BinaryPrimitives.ReadUInt64LittleEndian(data.AsSpan(offset));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong ReadU64Le(ReadOnlySpan<byte> span)
=> BinaryPrimitives.ReadUInt64LittleEndian(span);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong ReadU64Be(byte[] data, int offset)
=> BinaryPrimitives.ReadUInt64BigEndian(data.AsSpan(offset));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong ReadU64Be(ReadOnlySpan<byte> span)
=> BinaryPrimitives.ReadUInt64BigEndian(span);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteU64Le(byte[] data, int offset, ulong value)
=> BinaryPrimitives.WriteUInt64LittleEndian(data.AsSpan(offset), value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteU64Le(Span<byte> span, ulong value)
=> BinaryPrimitives.WriteUInt64LittleEndian(span, value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteU64Be(byte[] data, int offset, ulong value)
=> BinaryPrimitives.WriteUInt64BigEndian(data.AsSpan(offset), value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteU64Be(Span<byte> span, ulong value)
=> BinaryPrimitives.WriteUInt64BigEndian(span, value);
}

212
GCDTool/Program.cs Normal file
View File

@ -0,0 +1,212 @@
using CommandLine;
using CommandLine.Text;
using GCDTool.Elf;
using GCDTool.IO;
using GCDTool.Wram;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using System.Security.Cryptography;
using System.Text;
namespace GCDTool;
static class Program
{
private const string GCDTOOL_HEADING = "== GCDTool by Gericom ==";
private const string AES_CIPHER_NAME = "AES/CTR/NoPadding";
private const string ALGORITHM_AES = "AES";
private const int AES_BLOCK_SIZE = 16;
private const uint ROM_CONTROL = 0x00416657;
private const uint ROM_CONTROL_SECURE = 0x081808F8;
private const ushort SECURE_AREA_DELAY = 3454;
private static ReadOnlySpan<byte> AesKeyX
=> [0x4E, 0x69, 0x6E, 0x74, 0x65, 0x6E, 0x64, 0x6F, 0x20, 0x44, 0x53, 0x00, 0x01, 0x23, 0x21, 0x00];
private static void Main(string[] args)
{
var parser = new Parser(with => with.HelpWriter = null);
var parserResult = parser.ParseArguments<CommandLineOptions>(args);
parserResult
.WithParsed(RunOptions)
.WithNotParsed(_ =>
{
Console.WriteLine(HelpText.AutoBuild(parserResult, h =>
{
h.AutoVersion = false;
h.Copyright = string.Empty;
h.Heading = GCDTOOL_HEADING;
return HelpText.DefaultParsingErrorsHandler(parserResult, h);
}, e => e));
});
}
private static void RunOptions(CommandLineOptions options)
{
if (options.GameCode.Length != 4)
{
Console.WriteLine("Invalid game code specified. Should be 4 characters.");
return;
}
uint gameCode = IOUtil.ReadU32Le(Encoding.ASCII.GetBytes(options.GameCode));
// This can be any key, used for encryption of the arm7 and arm9 binaries.
// To have reproducable results, we'll simply use an all-zeros key.
byte[] aesKeyY = new byte[16];
var gcdRom = new GcdRom
{
Header = new GcdHeader
{
GameCode = gameCode,
RomControl = ROM_CONTROL,
RomControlSecure = ROM_CONTROL_SECURE,
SecureAreaDelay = SECURE_AREA_DELAY,
// it appears to be unnecessary to specify the rom area
NitroRomRegionEnd = 0,
TwlRomRegionStart = 0,
Flags = options.Arm9Speed67MHz ? 0 : GcdHeaderFlags.Arm9Speed134MHz
},
};
var signature = new GcdSignature();
aesKeyY.CopyTo(signature.AesKeyY, 0);
if (!TryLoadArm9(options.Arm9Path, gcdRom, signature) ||
!TryLoadArm7(options.Arm7Path, gcdRom, signature) ||
!TryLoadWramConfig(options.WramConfigJsonPath, gcdRom))
{
return;
}
gcdRom.Header.GetSha1Hash(signature.HeaderSha1);
var signedSignature = signature.ToSignedByteArray(options.GcdKeyPath);
signedSignature.CopyTo(gcdRom.Header.Signature, 0);
using (var stream = File.Create(options.OutputPath))
{
gcdRom.Write(stream);
}
}
private static bool TryLoadArm9(string arm9ElfPath, GcdRom gcdRom, GcdSignature signature)
{
var arm9Data = ParseElf(arm9ElfPath, out uint arm9LoadAddress);
if (arm9LoadAddress < 0x03000000)
{
Console.WriteLine("Error: ARM9 binary cannot be loaded to main memory. Use TWL wram instead.");
return false;
}
gcdRom.Header.Arm9LoadAddress = arm9LoadAddress;
gcdRom.Header.Arm9Size = (uint)arm9Data.Length;
SHA1.HashData(arm9Data, signature.Arm9Sha1);
if ((arm9Data.Length % 512) != 0)
{
arm9Data = [.. arm9Data, .. new byte[512 - (arm9Data.Length % 512)]];
}
gcdRom.Header.Arm9PaddedSize = (uint)arm9Data.Length;
EncryptAes(arm9Data, signature.AesKeyY);
gcdRom.EncryptedArm9Binary = arm9Data;
gcdRom.Header.Arm9RomOffset = 0x4000;
return true;
}
private static bool TryLoadArm7(string arm7ElfPath, GcdRom gcdRom, GcdSignature signature)
{
var arm7Data = ParseElf(arm7ElfPath, out uint arm7LoadAddress);
if (arm7LoadAddress < 0x03000000)
{
Console.WriteLine("Error: ARM7 binary cannot be loaded to main memory. Use TWL wram instead.");
return false;
}
gcdRom.Header.Arm7LoadAddress = arm7LoadAddress;
gcdRom.Header.Arm7Size = (uint)arm7Data.Length;
SHA1.HashData(arm7Data, signature.Arm7Sha1);
if ((arm7Data.Length % 512) != 0)
{
arm7Data = [.. arm7Data, .. new byte[512 - (arm7Data.Length % 512)]];
}
gcdRom.Header.Arm7PaddedSize = (uint)arm7Data.Length;
EncryptAes(arm7Data, signature.AesKeyY);
gcdRom.EncryptedArm7Binary = arm7Data;
gcdRom.Header.Arm7RomOffset = (uint)(0x4000 + gcdRom.EncryptedArm9Binary.Length);
return true;
}
private static byte[] ParseElf(string elfPath, out uint loadAddress)
{
loadAddress = 0;
var elf = new ElfFile(File.ReadAllBytes(elfPath));
var programData = new MemoryStream();
bool loadAddressSet = false;
foreach (var programHeader in elf.ProgramHeaderTable)
{
if (programHeader.SegmentType == ProgramHeaderTableEntry.ElfSegmentType.Load &&
programHeader.SegmentData is not null &&
programHeader.SegmentData.Length != 0)
{
if (!loadAddressSet)
{
loadAddress = programHeader.PhysicalAddress;
loadAddressSet = true;
}
programData.Write(programHeader.SegmentData);
}
}
return programData.ToArray();
}
private static void ByteSwapAesBlocks(Span<byte> data)
{
for (int i = 0; i < data.Length; i += AES_BLOCK_SIZE)
{
data.Slice(i, AES_BLOCK_SIZE).Reverse();
}
}
private static byte[] CreateAesInitializationVector(uint dataLength)
{
var iv = new byte[16];
IOUtil.WriteU32Le(iv, 0, dataLength);
IOUtil.WriteU32Le(iv, 4, (uint)-dataLength);
IOUtil.WriteU32Le(iv, 8, ~dataLength);
return iv;
}
private static void EncryptAes(Span<byte> data, ReadOnlySpan<byte> keyY)
{
var scrambledKey = AesKeyScrambler.Scramble(AesKeyX, keyY);
Array.Reverse(scrambledKey);
ByteSwapAesBlocks(data);
var iv = CreateAesInitializationVector((uint)data.Length);
Array.Reverse(iv);
var cipher = CipherUtilities.GetCipher(AES_CIPHER_NAME);
cipher.Init(true, new ParametersWithIV(
ParameterUtilities.CreateKeyParameter(ALGORITHM_AES, scrambledKey), iv));
cipher.ProcessBytes(data, data);
cipher.DoFinal(data);
ByteSwapAesBlocks(data);
}
private static bool TryLoadWramConfig(string wramConfigJsonPath, GcdRom gcdRom)
{
try
{
string wramConfigJson = File.ReadAllText(wramConfigJsonPath);
gcdRom.Header.WramConfiguration = new WramConfigJsonReader().ReadWramConfigFromJson(wramConfigJson);
return true;
}
catch (Exception exception)
{
Console.WriteLine("Failed to parse wram configuration JSON: " + exception.Message);
return false;
}
}
}

View File

@ -0,0 +1,10 @@
namespace GCDTool.Wram;
/// <summary>
/// The master of a NTR wram block.
/// </summary>
enum NtrWramMaster
{
Arm9 = 0,
Arm7 = 1
}

View File

@ -0,0 +1,14 @@
namespace GCDTool.Wram;
/// <summary>
/// Specifies the mapping of TWL wram A block.
/// </summary>
readonly record struct WramABlockMapping(WramAMaster Master, WramASlot Slot, bool Enable)
{
public static explicit operator byte(WramABlockMapping wramABlockMapping)
{
return (byte)(((byte)wramABlockMapping.Master & 1)
| (((byte)wramABlockMapping.Slot & 3) << 2)
| (wramABlockMapping.Enable ? 0x80 : 0));
}
}

View File

@ -0,0 +1,22 @@
namespace GCDTool.Wram;
/// <summary>
/// Specifies which TWL wram A slots are mapped in memory.
/// </summary>
enum WramAMappedSlots
{
/// <summary>
/// Only slot 0 mapped. 64 kB mirrors.
/// </summary>
Slot0 = 0,
/// <summary>
/// Slot 0 and 1 mapped. 128 kB mirrors.
/// </summary>
Slot01 = 2,
/// <summary>
/// All slots mapped. 256 kB mirrors.
/// </summary>
All = 3
}

View File

@ -0,0 +1,63 @@
namespace GCDTool.Wram;
/// <summary>
/// Specifies the mapping of TWL wram A in memory.
/// </summary>
readonly record struct WramAMapping
{
private const uint WRAM_START_ADDRESS = 0x03000000u;
private const uint WRAM_END_ADDRESS = 0x04000000u;
private readonly uint _startOffset;
private readonly uint _endOffset;
/// <summary>
/// The WRAM A slots that are mapped and mirrored in memory.
/// </summary>
public WramAMappedSlots MappedSlots { get; }
/// <summary>
/// The start address of the WRAM A area in memory.
/// </summary>
public uint StartAddress => WRAM_START_ADDRESS + (_startOffset << 16);
/// <summary>
/// The end address of the WRAM A area in memory.
/// </summary>
public uint EndAddress => WRAM_START_ADDRESS + (_endOffset << 16);
public WramAMapping(uint startAddress, uint length, WramAMappedSlots mappedSlots)
{
if (startAddress < WRAM_START_ADDRESS)
{
throw new ArgumentException(
$"Start address (0x{startAddress:X8}) must be at least (0x{WRAM_START_ADDRESS:X8}).", nameof(startAddress));
}
if ((startAddress & 0xFFFF) != 0)
{
throw new ArgumentException("Start address must be a multiple of 64 kB.", nameof(startAddress));
}
if ((length & 0xFFFF) != 0)
{
throw new ArgumentException("Length must be a multiple of 64 kB.", nameof(length));
}
uint endAddress = startAddress + length;
if (endAddress > WRAM_END_ADDRESS)
{
throw new ArgumentException(
$"Length is too large. Resulting end address (0x{endAddress:X8}) must be at most (0x{WRAM_END_ADDRESS:X8}).",
nameof(length));
}
_startOffset = (startAddress - WRAM_START_ADDRESS) >> 16;
_endOffset = (endAddress - WRAM_START_ADDRESS) >> 16;
MappedSlots = mappedSlots;
}
public static explicit operator uint(WramAMapping wramAMapping)
{
return (wramAMapping._startOffset << 4)
| ((uint)wramAMapping.MappedSlots << 12)
| (wramAMapping._endOffset << 20);
}
}

View File

@ -0,0 +1,10 @@
namespace GCDTool.Wram;
/// <summary>
/// The master of a TWL wram A block.
/// </summary>
enum WramAMaster
{
Arm9 = 0,
Arm7 = 1
}

12
GCDTool/Wram/WramASlot.cs Normal file
View File

@ -0,0 +1,12 @@
namespace GCDTool.Wram;
/// <summary>
/// Specifies a slot of TWL wram A.
/// </summary>
enum WramASlot
{
Slot0 = 0,
Slot1 = 1,
Slot2 = 2,
Slot3 = 3
}

View File

@ -0,0 +1,14 @@
namespace GCDTool.Wram;
/// <summary>
/// Specifies the mapping of TWL wram B block.
/// </summary>
readonly record struct WramBBlockMapping(WramBMaster Master, WramBCSlot Slot, bool Enable)
{
public static explicit operator byte(WramBBlockMapping wramBBlockMapping)
{
return (byte)(((byte)wramBBlockMapping.Master & 3)
| (((byte)wramBBlockMapping.Slot & 7) << 2)
| (wramBBlockMapping.Enable ? 0x80 : 0));
}
}

View File

@ -0,0 +1,27 @@
namespace GCDTool.Wram;
/// <summary>
/// Specifies which TWL wram B or C slots are mapped in memory.
/// </summary>
enum WramBCMappedSlots
{
/// <summary>
/// Only slot 0 mapped. 32 kB mirrors.
/// </summary>
Slot0 = 0,
/// <summary>
/// Only slot 0 and 1 mapped. 64 kB mirrors.
/// </summary>
Slot01 = 1,
/// <summary>
/// Only slot 0, 1, 2 and 3 mapped. 128 kB mirrors.
/// </summary>
Slot0123 = 2,
/// <summary>
/// All slots mapped. 256 kB mirrors.
/// </summary>
All = 3
}

View File

@ -0,0 +1,63 @@
namespace GCDTool.Wram;
/// <summary>
/// Specifies the mapping of TWL wram B or C in memory.
/// </summary>
readonly record struct WramBCMapping
{
private const uint WRAM_START_ADDRESS = 0x03000000u;
private const uint WRAM_END_ADDRESS = 0x04000000u;
private readonly uint _startOffset;
private readonly uint _endOffset;
/// <summary>
/// The WRAM A slots that are mapped and mirrored in memory.
/// </summary>
public WramBCMappedSlots MappedSlots { get; }
/// <summary>
/// The start address of the WRAM B or C area in memory.
/// </summary>
public uint StartAddress => WRAM_START_ADDRESS + (_startOffset << 15);
/// <summary>
/// The end address of the WRAM B or C area in memory.
/// </summary>
public uint EndAddress => WRAM_START_ADDRESS + (_endOffset << 15);
public WramBCMapping(uint startAddress, uint length, WramBCMappedSlots mappedSlots)
{
if (startAddress < WRAM_START_ADDRESS)
{
throw new ArgumentException(
$"Start address (0x{startAddress:X8}) must be at least (0x{WRAM_START_ADDRESS:X8}).", nameof(startAddress));
}
if ((startAddress & 0x7FFF) != 0)
{
throw new ArgumentException("Start address must be a multiple of 32 kB.", nameof(startAddress));
}
if ((length & 0x7FFF) != 0)
{
throw new ArgumentException("Length must be a multiple of 32 kB.", nameof(length));
}
uint endAddress = startAddress + length;
if (endAddress > WRAM_END_ADDRESS)
{
throw new ArgumentException(
$"Length is too large. Resulting end address (0x{endAddress:X8}) must be at most (0x{WRAM_END_ADDRESS:X8}).",
nameof(length));
}
_startOffset = (startAddress - WRAM_START_ADDRESS) >> 15;
_endOffset = (endAddress - WRAM_START_ADDRESS) >> 15;
MappedSlots = mappedSlots;
}
public static explicit operator uint(WramBCMapping wramBCMapping)
{
return (wramBCMapping._startOffset << 3)
| ((uint)wramBCMapping.MappedSlots << 12)
| (wramBCMapping._endOffset << 19);
}
}

View File

@ -0,0 +1,16 @@
namespace GCDTool.Wram;
/// <summary>
/// Specifies a slot of TWL wram B or C.
/// </summary>
enum WramBCSlot
{
Slot0 = 0,
Slot1 = 1,
Slot2 = 2,
Slot3 = 3,
Slot4 = 4,
Slot5 = 5,
Slot6 = 6,
Slot7 = 7
}

View File

@ -0,0 +1,11 @@
namespace GCDTool.Wram;
/// <summary>
/// The master of a TWL wram B block.
/// </summary>
enum WramBMaster
{
Arm9 = 0,
Arm7 = 1,
DspCode = 2
}

View File

@ -0,0 +1,14 @@
namespace GCDTool.Wram;
/// <summary>
/// Specifies the mapping of TWL wram C block.
/// </summary>
readonly record struct WramCBlockMapping(WramCMaster Master, WramBCSlot Slot, bool Enable)
{
public static explicit operator byte(WramCBlockMapping wramCBlockMapping)
{
return (byte)(((byte)wramCBlockMapping.Master & 3)
| (((byte)wramCBlockMapping.Slot & 7) << 2)
| (wramCBlockMapping.Enable ? 0x80 : 0));
}
}

View File

@ -0,0 +1,11 @@
namespace GCDTool.Wram;
/// <summary>
/// The master of a TWL wram C block.
/// </summary>
enum WramCMaster
{
Arm9 = 0,
Arm7 = 1,
DspData = 2
}

126
GCDTool/Wram/WramConfig.cs Normal file
View File

@ -0,0 +1,126 @@
using GCDTool.IO;
namespace GCDTool.Wram;
/// <summary>
/// Class representing the initial wram configuration.
/// </summary>
sealed class WramConfig
{
/// <summary>
/// The block mapping of TWL wram A.
/// </summary>
public WramABlockMapping[] TwlWramABlockMapping { get; } = new WramABlockMapping[4];
/// <summary>
/// The block mapping of TWL wram B.
/// </summary>
public WramBBlockMapping[] TwlWramBBlockMapping { get; } = new WramBBlockMapping[8];
/// <summary>
/// The block mapping of TWL wram C.
/// </summary>
public WramCBlockMapping[] TwlWramCBlockMapping { get; } = new WramCBlockMapping[8];
/// <summary>
/// The mapping of TWL wram A on the ARM9.
/// </summary>
public WramAMapping Arm9TwlWramAMapping { get; set; }
/// <summary>
/// The mapping of TWL wram B on the ARM9.
/// </summary>
public WramBCMapping Arm9TwlWramBMapping { get; set; }
/// <summary>
/// The mapping of TWL wram C on the ARM9.
/// </summary>
public WramBCMapping Arm9TwlWramCMapping { get; set; }
/// <summary>
/// The mapping of TWL wram A on the ARM7.
/// </summary>
public WramAMapping Arm7TwlWramAMapping { get; set; }
/// <summary>
/// The mapping of TWL wram B on the ARM7.
/// </summary>
public WramBCMapping Arm7TwlWramBMapping { get; set; }
/// <summary>
/// The mapping of TWL wram C on the ARM7.
/// </summary>
public WramBCMapping Arm7TwlWramCMapping { get; set; }
/// <summary>
/// For each TWL wram A block <see langword="true"/> when locked, or <see langword="false"/> otherwise.
/// </summary>
public bool[] TwlWramABlockLocked { get; } = new bool[4];
/// <summary>
/// For each TWL wram B block <see langword="true"/> when locked, or <see langword="false"/> otherwise.
/// </summary>
public bool[] TwlWramBBlockLocked { get; } = new bool[8];
/// <summary>
/// For each TWL wram C block <see langword="true"/> when locked, or <see langword="false"/> otherwise.
/// </summary>
public bool[] TwlWramCBlockLocked { get; } = new bool[8];
/// <summary>
/// The mapping of nitro wram.
/// </summary>
public NtrWramMaster[] NtrWramMapping { get; } = new NtrWramMaster[2];
/// <summary>
/// The mapping of vram C.
/// </summary>
public byte VramCMapping { get; set; }
/// <summary>
/// The mapping of vram D.
/// </summary>
public byte VramDMapping { get; set; }
/// <summary>
/// Writes the wram configuration to the given <paramref name="destination"/> span.
/// </summary>
/// <param name="destination"></param>
public void Write(Span<byte> destination)
{
for (int i = 0; i < 4; i++)
{
destination[i] = (byte)TwlWramABlockMapping[i];
}
for (int i = 0; i < 8; i++)
{
destination[4 + i] = (byte)TwlWramBBlockMapping[i];
destination[12 + i] = (byte)TwlWramCBlockMapping[i];
}
IOUtil.WriteU32Le(destination[0x14..], (uint)Arm9TwlWramAMapping);
IOUtil.WriteU32Le(destination[0x18..], (uint)Arm9TwlWramBMapping);
IOUtil.WriteU32Le(destination[0x1C..], (uint)Arm9TwlWramCMapping);
IOUtil.WriteU32Le(destination[0x20..], (uint)Arm7TwlWramAMapping);
IOUtil.WriteU32Le(destination[0x24..], (uint)Arm7TwlWramBMapping);
IOUtil.WriteU32Le(destination[0x28..], (uint)Arm7TwlWramCMapping);
destination[0x2C] = CreateLockMask(TwlWramABlockLocked);
destination[0x2D] = CreateLockMask(TwlWramBBlockLocked);
destination[0x2E] = CreateLockMask(TwlWramCBlockLocked);
destination[0x2F] = (byte)(((byte)NtrWramMapping[0] & 1) | (((byte)NtrWramMapping[1] & 1) << 1)
| (VramCMapping & 7) << 2 | (VramDMapping & 7) << 5);
}
private byte CreateLockMask(ReadOnlySpan<bool> locks)
{
byte wramLocks = 0;
for (int i = 0; i < locks.Length; i++)
{
if (locks[i])
{
wramLocks |= (byte)(1 << i);
}
}
return wramLocks;
}
}

View File

@ -0,0 +1,254 @@
using System.ComponentModel;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace GCDTool.Wram;
/// <summary>
/// Class for reading <see cref="WramConfig"/> from a JSON file.
/// </summary>
sealed class WramConfigJsonReader
{
private const int WRAM_A_BLOCK_COUNT = 4;
private const int WRAM_BC_BLOCK_COUNT = 8;
private const string MASTER_ARM9 = "arm9";
private const string MASTER_ARM7 = "arm7";
private const string MASTER_DSP = "dsp";
private const string SLOT_0 = "0";
private const string SLOT_01 = "01";
private const string SLOT_0123 = "0123";
private const string SLOT_01234567 = "01234567";
private const string SLOT_ALL = "all";
/// <summary>
/// Reads a <see cref="WramConfig"/> from the given <paramref name="jsonString"/>.
/// </summary>
/// <param name="jsonString">The JSON string containing the WRAM configuration.</param>
/// <returns>The constructed <see cref="WramConfig"/>.</returns>
public WramConfig ReadWramConfigFromJson(string jsonString)
{
var wramConfig = new WramConfig();
var json = JsonDocument.Parse(jsonString).RootElement;
ParseWramAConfig(wramConfig, json.GetProperty("wramA"));
ParseWramBConfig(wramConfig, json.GetProperty("wramB"));
ParseWramCConfig(wramConfig, json.GetProperty("wramC"));
var ntrWram = json.GetProperty("ntrWram");
if (ntrWram.GetArrayLength() != 2)
{
throw new InvalidDataException("'ntrWram' array should have exactly 2 items.");
}
wramConfig.NtrWramMapping[0] = ParseNtrWramMapping(ntrWram[0].GetString());
wramConfig.NtrWramMapping[1] = ParseNtrWramMapping(ntrWram[1].GetString());
wramConfig.VramCMapping = json.GetProperty("vramC").GetByte();
wramConfig.VramDMapping = json.GetProperty("vramD").GetByte();
return wramConfig;
}
private void ParseWramAConfig(WramConfig wramConfig, JsonElement wramAJson)
{
wramConfig.Arm9TwlWramAMapping = wramAJson.TryGetProperty("arm9", out var arm9)
? ParseWramAMapping(arm9)
: new WramAMapping(0x03000000, 0, WramAMappedSlots.Slot0); // unmapped
wramConfig.Arm7TwlWramAMapping = wramAJson.TryGetProperty("arm7", out var arm7)
? ParseWramAMapping(arm7)
: new WramAMapping(0x03000000, 0, WramAMappedSlots.Slot0); // unmapped
var blocks = wramAJson.GetProperty("blocks");
if (blocks.GetArrayLength() != WRAM_A_BLOCK_COUNT)
{
throw new InvalidDataException($"Wram A 'blocks' array should have exactly {WRAM_A_BLOCK_COUNT} items.");
}
for (int i = 0; i < WRAM_A_BLOCK_COUNT; i++)
{
wramConfig.TwlWramABlockMapping[i] = ParseWramABlockMapping(
blocks[i], out wramConfig.TwlWramABlockLocked[i]);
}
}
private void ParseWramBConfig(WramConfig wramConfig, JsonElement wramBJson)
{
wramConfig.Arm9TwlWramBMapping = wramBJson.TryGetProperty("arm9", out var arm9)
? ParseWramBCMapping(arm9)
: new WramBCMapping(0x03000000, 0, WramBCMappedSlots.Slot0); // unmapped
wramConfig.Arm7TwlWramBMapping = wramBJson.TryGetProperty("arm7", out var arm7)
? ParseWramBCMapping(arm7)
: new WramBCMapping(0x03000000, 0, WramBCMappedSlots.Slot0); // unmapped
var blocks = wramBJson.GetProperty("blocks");
if (blocks.GetArrayLength() != WRAM_BC_BLOCK_COUNT)
{
throw new InvalidDataException($"Wram B 'blocks' array should have exactly {WRAM_BC_BLOCK_COUNT} items.");
}
for (int i = 0; i < WRAM_BC_BLOCK_COUNT; i++)
{
wramConfig.TwlWramBBlockMapping[i] = ParseWramBBlockMapping(
blocks[i], out wramConfig.TwlWramBBlockLocked[i]);
}
}
private void ParseWramCConfig(WramConfig wramConfig, JsonElement wramCJson)
{
wramConfig.Arm9TwlWramCMapping = wramCJson.TryGetProperty("arm9", out var arm9)
? ParseWramBCMapping(arm9)
: new WramBCMapping(0x03000000, 0, WramBCMappedSlots.Slot0); // unmapped
wramConfig.Arm7TwlWramCMapping = wramCJson.TryGetProperty("arm7", out var arm7)
? ParseWramBCMapping(arm7)
: new WramBCMapping(0x03000000, 0, WramBCMappedSlots.Slot0); // unmapped
var blocks = wramCJson.GetProperty("blocks");
if (blocks.GetArrayLength() != WRAM_BC_BLOCK_COUNT)
{
throw new InvalidDataException($"Wram C 'blocks' array should have exactly {WRAM_BC_BLOCK_COUNT} items.");
}
for (int i = 0; i < WRAM_BC_BLOCK_COUNT; i++)
{
wramConfig.TwlWramCBlockMapping[i] = ParseWramCBlockMapping(
blocks[i], out wramConfig.TwlWramCBlockLocked[i]);
}
}
private WramAMapping ParseWramAMapping(JsonElement mappingJson)
{
var wramMapping = mappingJson.Deserialize<JsonWramMapping>()
?? throw new InvalidDataException("Could not parse wram A mapping.");
return new(
ParseHexString(wramMapping.Start),
ParseHexString(wramMapping.Length),
wramMapping.Slots switch
{
SLOT_0 => WramAMappedSlots.Slot0,
SLOT_01 => WramAMappedSlots.Slot01,
SLOT_0123 or SLOT_ALL => WramAMappedSlots.All,
_ => throw new InvalidDataException("Invalid value specified for wram A 'slots'.")
});
}
private WramBCMapping ParseWramBCMapping(JsonElement mappingJson)
{
var wramMapping = mappingJson.Deserialize<JsonWramMapping>()
?? throw new InvalidDataException("Could not parse wram B or C mapping.");
return new(
ParseHexString(wramMapping.Start),
ParseHexString(wramMapping.Length),
wramMapping.Slots switch
{
SLOT_0 => WramBCMappedSlots.Slot0,
SLOT_01 => WramBCMappedSlots.Slot01,
SLOT_0123 => WramBCMappedSlots.Slot0123,
SLOT_01234567 or SLOT_ALL => WramBCMappedSlots.All,
_ => throw new InvalidDataException("Invalid value specified for wram B or C 'slots'.")
});
}
private WramABlockMapping ParseWramABlockMapping(JsonElement blockMappingJson, out bool locked)
{
var blockMapping = blockMappingJson.Deserialize<JsonBlockMapping>()
?? throw new InvalidDataException("Could not parse wram A block mapping.");
if (blockMapping.Slot >= WRAM_A_BLOCK_COUNT)
{
throw new InvalidDataException("Invalid slot specified for wram A block mapping.");
}
locked = blockMapping.Locked;
return new(
blockMapping.Master switch
{
MASTER_ARM7 => WramAMaster.Arm7,
MASTER_ARM9 => WramAMaster.Arm9,
_ => throw new InvalidDataException("Invalid master specified for wram A block mapping.")
},
(WramASlot)blockMapping.Slot,
blockMapping.Enabled);
}
private WramBBlockMapping ParseWramBBlockMapping(JsonElement blockMappingJson, out bool locked)
{
var blockMapping = blockMappingJson.Deserialize<JsonBlockMapping>()
?? throw new InvalidDataException("Could not parse wram B block mapping.");
if (blockMapping.Slot >= WRAM_BC_BLOCK_COUNT)
{
throw new InvalidDataException("Invalid slot specified for wram B block mapping.");
}
locked = blockMapping.Locked;
return new(
blockMapping.Master switch
{
MASTER_ARM9 => WramBMaster.Arm9,
MASTER_ARM7 => WramBMaster.Arm7,
MASTER_DSP => WramBMaster.DspCode,
_ => throw new InvalidDataException("Invalid master specified for wram B block mapping.")
},
(WramBCSlot)blockMapping.Slot,
blockMapping.Enabled);
}
private WramCBlockMapping ParseWramCBlockMapping(JsonElement blockMappingJson, out bool locked)
{
var blockMapping = blockMappingJson.Deserialize<JsonBlockMapping>()
?? throw new InvalidDataException("Could not parse wram C block mapping.");
if (blockMapping.Slot >= WRAM_BC_BLOCK_COUNT)
{
throw new InvalidDataException("Invalid slot specified for wram C block mapping.");
}
locked = blockMapping.Locked;
return new(
blockMapping.Master switch
{
MASTER_ARM9 => WramCMaster.Arm9,
MASTER_ARM7 => WramCMaster.Arm7,
MASTER_DSP => WramCMaster.DspData,
_ => throw new InvalidDataException("Invalid master specified for wram C block mapping.")
},
(WramBCSlot)blockMapping.Slot,
blockMapping.Enabled);
}
private NtrWramMaster ParseNtrWramMapping(string? mapping) => mapping switch
{
MASTER_ARM9 => NtrWramMaster.Arm9,
MASTER_ARM7 => NtrWramMaster.Arm7,
_ => throw new InvalidDataException("Invalid master specified for ntr wram mapping.")
};
private uint ParseHexString(string hexString)
{
if (new UInt32Converter().ConvertFromString(hexString) is not uint value)
{
throw new InvalidDataException("Invalid start address or length specified.");
}
return value;
}
private sealed class JsonWramMapping
{
[JsonPropertyName("start")]
public string Start { get; set; } = "0x03000000";
[JsonPropertyName("length")]
public string Length { get; set; } = "0";
[JsonPropertyName("slots")]
public string Slots { get; set; } = SLOT_0;
}
private sealed class JsonBlockMapping
{
[JsonPropertyName("master")]
public string Master { get; set; } = MASTER_ARM9;
[JsonPropertyName("slot")]
public uint Slot { get; set; } = 0;
[JsonPropertyName("enabled")]
public bool Enabled { get; set; } = true;
[JsonPropertyName("locked")]
public bool Locked { get; set; } = false;
}
}

88
readme.md Normal file
View File

@ -0,0 +1,88 @@
# GCDTool
Tool to create a GCD rom for DSi ntrboot.
## Usage
`GCDTool --arm9 arm9.elf --arm7 arm7.elf --wram wramConfig.json --rsakey gcdKey.der output.gcd`
### Arguments
#### Mandatory
- `--arm9` specifies the arm9 elf file;
- `--arm7` specifies the arm7 elf file;
- `--wram` specifies the wram configuration json file (see below);
- `--rsakey` specifies the der file with the RSA private key to use.
The arm7 and arm9 binaries should have a load address in internal ram (for example TWL wram), as main memory is not yet enabled when the GCD rom is booted.
#### Optional
- `--gamecode` specifies the 4 character game code to use (default: ####);
- `--arm9-67-mhz` runs the arm9 at 67 MHz instead of 134 MHz.
### Wram configuration JSON file
Specifies the initial wram configuration to use.
#### Example
```json
{
"wramA": {
"arm7": { "start": "0x03800000", "length": "0x40000", "slots": "all" },
"blocks": [
{ "master": "arm7", "slot": 0 },
{ "master": "arm7", "slot": 1 },
{ "master": "arm7", "slot": 2 },
{ "master": "arm7", "slot": 3 }
]
},
"wramB": {
"arm9": { "start": "0x03800000", "length": "0x40000", "slots": "all" },
"blocks": [
{ "master": "arm9", "slot": 0 },
{ "master": "arm9", "slot": 1 },
{ "master": "arm9", "slot": 2 },
{ "master": "arm9", "slot": 3 },
{ "master": "arm9", "slot": 4 },
{ "master": "arm9", "slot": 5 },
{ "master": "arm9", "slot": 6 },
{ "master": "arm9", "slot": 7 }
]
},
"wramC": {
"arm9": { "start": "0x03840000", "length": "0x40000", "slots": "all" },
"blocks": [
{ "master": "arm9", "slot": 0 },
{ "master": "arm9", "slot": 1 },
{ "master": "arm9", "slot": 2 },
{ "master": "arm9", "slot": 3 },
{ "master": "arm9", "slot": 4 },
{ "master": "arm9", "slot": 5 },
{ "master": "arm9", "slot": 6 },
{ "master": "arm9", "slot": 7 }
]
},
"ntrWram" : [ "arm7", "arm7" ],
"vramC": 7,
"vramD": 7
}
```
#### TWL wram mapping
For TWL wram A, B and C the mapping in memory can be specified for both the arm9 and arm7 side with the `"arm9"` and `"arm7"` properties (can be omitted to not map the ram at all). Start and length must be 64 kB aligned for wram A, and 32 kB aligned for wram B and C. `"slots"` specifies which slots are actually mapped in memory, and as such also the size of the mirroring. For wram A the options are `"0"` (64 kB), `"01"` (128 kB) and `"all"` or `"0123"` (256 kB). For wram B and C the options are `"0"` (32 kB), `"01"` (64 kB), `"0123"` (128 kB) and `"all"` or `"01234567"` (256 kB).
#### TWL wram block mapping
For each block of TWL wram the following properties are available:
- `"master"` specifies what the block is mapped to. All wrams support `"arm9"` and `"arm7"`, and wram B and C additionally support `"dsp"` for dsp code and data respectively;
- `"slot"` specifies the slot the block is mapped to;
- `"enabled"` specifies whether the block is enabled. This defaults to `true` and can be omitted;
- `"locked"` specifies whether the mapping of the block should be locked, such that the arm9 cannot change it. This defaults to `false` and can be omitted.
When `"enabled"` is set to `false`, `"master"` and `"slot"` can be omitted.
#### NTR wram mapping
Specifies for the two NTR wram blocks whether they are mapped to `"arm9"` or `"arm7"`.
#### Vram mapping
Not sure how this works. There are only 3 bits available, but the vram C and D mapping registers need more bits properly specify the mapping and enable it. Default value is 7 for both.
## Booting the GCD rom
The GCD rom needs to be flashed to a (dev) flashcard or other means of emulating a DS cartridge. Make sure the blowfish table is used that is included in the generated .gcd file (p table at `0x1600` and s boxes at `0x1C00`).
With the cartridge inserted, the GCD rom is booted by putting a small magnet between the ABXY buttons (to trigger the lid close detection), holding START, SELECT and X and pressing the power button.