NdsForge.NET 1.0.1
Read, validate, edit, compare, and build Nintendo DS and DSi images from .NET
Loading...
Searching...
No Matches
NdsDsiHeader.cs
1namespace NdsForge;
2
4public sealed class NdsDsiHeader
5{
7 private readonly ReadOnlyMemory<byte> _rawHeader;
10 internal NdsDsiHeader(ReadOnlyMemory<byte> rawHeader)
11 {
12 _rawHeader = rawHeader;
13 ReadOnlySpan<byte> data = rawHeader.Span;
14 RawData = rawHeader.Slice(0x180, 0xE80);
15 MemoryBankSettings = rawHeader.Slice(0x180, 0x30);
16 RegionFlags = NdsBinary.ReadUInt32(data, 0x1B0);
17 AccessControl = NdsBinary.ReadUInt32(data, 0x1B4);
18 ScfgExtMask = NdsBinary.ReadUInt32(data, 0x1B8);
19 ApplicationFlags = data[0x1BF];
20 Arm7DeviceListAddress = NdsBinary.ReadUInt32(data, 0x1D4);
21 NtrDigest = ReadRegion(data, 0x1E0);
22 TwlDigest = ReadRegion(data, 0x1E8);
23 SectorHashTable = ReadRegion(data, 0x1F0);
24 BlockHashTable = ReadRegion(data, 0x1F8);
25 DigestSectorSize = NdsBinary.ReadUInt32(data, 0x200);
26 DigestBlockSectorCount = NdsBinary.ReadUInt32(data, 0x204);
27 BannerSize = NdsBinary.ReadUInt32(data, 0x208);
28 TotalImageSize = NdsBinary.ReadUInt32(data, 0x210);
29 ModcryptArea1 = ReadRegion(data, 0x220);
30 ModcryptArea2 = ReadRegion(data, 0x228);
31 uint titleIdLow = NdsBinary.ReadUInt32(data, 0x230);
32 uint titleIdHigh = NdsBinary.ReadUInt32(data, 0x234);
33 TitleId = ((ulong)titleIdHigh << 32) | titleIdLow;
34 PublicSaveSize = NdsBinary.ReadUInt32(data, 0x238);
35 PrivateSaveSize = NdsBinary.ReadUInt32(data, 0x23C);
36 AgeRatings = rawHeader.Slice(0x2F0, 0x10);
37 Arm9Hmac = rawHeader.Slice(0x300, 20);
38 Arm7Hmac = rawHeader.Slice(0x314, 20);
39 DigestMasterHmac = rawHeader.Slice(0x328, 20);
40 BannerHmac = rawHeader.Slice(0x33C, 20);
41 Arm9iHmac = rawHeader.Slice(0x350, 20);
42 Arm7iHmac = rawHeader.Slice(0x364, 20);
43 Arm9WithoutSecureAreaHmac = rawHeader.Slice(0x3A0, 20);
44 DebugArguments = rawHeader.Slice(0xE00, 0x180);
45 RsaSignature = rawHeader.Slice(0xF80, 0x80);
46 }
47
49 public ReadOnlyMemory<byte> RawData { get; }
50
52 public ReadOnlyMemory<byte> MemoryBankSettings { get; }
53
55 public uint RegionFlags { get; }
56
58 public uint AccessControl { get; }
59
61 public uint ScfgExtMask { get; }
62
64 public byte ApplicationFlags { get; }
65
67 public uint Arm7DeviceListAddress { get; }
68
70 public NdsRegion NtrDigest { get; }
71
73 public NdsRegion TwlDigest { get; }
74
76 public NdsRegion SectorHashTable { get; }
77
79 public NdsRegion BlockHashTable { get; }
80
82 public uint DigestSectorSize { get; }
83
85 public uint DigestBlockSectorCount { get; }
86
88 public uint BannerSize { get; }
89
91 public uint TotalImageSize { get; }
92
94 public NdsRegion ModcryptArea1 { get; }
95
97 public NdsRegion ModcryptArea2 { get; }
98
100 public bool HasModcryptAreas => !ModcryptArea1.IsEmpty || !ModcryptArea2.IsEmpty;
101
107 (_rawHeader.Span[0x1C] & 0x04) != 0 || (ApplicationFlags & 0x80) != 0;
108
112 public NdsRegion GetModcryptArea(NdsModcryptArea area) => area switch
113 {
116 _ => throw new ArgumentOutOfRangeException(nameof(area), area, "Unknown modcrypt area."),
117 };
118
120 public ulong TitleId { get; }
121
123 public uint PublicSaveSize { get; }
124
126 public uint PrivateSaveSize { get; }
127
129 public ReadOnlyMemory<byte> AgeRatings { get; }
130
132 public ReadOnlyMemory<byte> Arm9Hmac { get; }
133
135 public ReadOnlyMemory<byte> Arm7Hmac { get; }
136
138 public ReadOnlyMemory<byte> DigestMasterHmac { get; }
139
141 public ReadOnlyMemory<byte> BannerHmac { get; }
142
144 public ReadOnlyMemory<byte> Arm9iHmac { get; }
145
147 public ReadOnlyMemory<byte> Arm7iHmac { get; }
148
150 public ReadOnlyMemory<byte> Arm9WithoutSecureAreaHmac { get; }
151
153 public ReadOnlyMemory<byte> DebugArguments { get; }
154
156 public ReadOnlyMemory<byte> RsaSignature { get; }
157
165 {
166 ArgumentNullException.ThrowIfNull(publicKey);
167 return publicKey.VerifyHeader(_rawHeader.Span[..0xE00], RsaSignature.Span);
168 }
169
174 private static NdsRegion ReadRegion(ReadOnlySpan<byte> data, int offset) =>
175 NdsRegion.FromUInt32(NdsBinary.ReadUInt32(data, offset), NdsBinary.ReadUInt32(data, offset + 4));
176}
uint BannerSize
Declares the DSi banner allocation, including animated data when the version is 0x0103.
uint DigestBlockSectorCount
Defines how many sector digests contribute to each entry in BlockHashTable.
NdsRegion NtrDigest
Locates the NTR-mode content range covered by the hierarchical DSi digest tables.
uint PublicSaveSize
Declares public writable storage bytes requested by DSi software, not an embedded ROM region.
ReadOnlyMemory< byte > RsaSignature
Preserves the 128-byte header signature for later verification without claiming trust or key provenan...
ReadOnlyMemory< byte > BannerHmac
Contains the 20-byte SHA-1 HMAC authenticating DSi banner bytes when required by title metadata.
ReadOnlyMemory< byte > MemoryBankSettings
Gets raw global, ARM9, ARM7, and WRAM memory-bank settings.
NdsRegion SectorHashTable
Locates the SHA-1 sector digest array whose grouping is defined by DigestSectorSize.
ReadOnlyMemory< byte > AgeRatings
Preserves sixteen territory-specific rating bytes because their flags and authorities vary by slot.
uint ScfgExtMask
Preserves the SCFG_EXT mask controlling which DSi hardware configuration bits software may change.
bool UsesInsecureModcryptKey
Indicates that modcrypt uses the public first sixteen header bytes instead of a securely derived norm...
ReadOnlyMemory< byte > Arm9iHmac
Contains the 20-byte SHA-1 HMAC authenticating the DSi-mode ARM9i payload.
uint DigestSectorSize
Defines the content byte granularity represented by each entry in SectorHashTable.
ReadOnlyMemory< byte > Arm9Hmac
Contains the 20-byte SHA-1 HMAC authenticating the common ARM9 payload; no key-validity claim is impl...
NdsRegion GetModcryptArea(NdsModcryptArea area)
Selects one declared modcrypt interval while rejecting undefined enum values at the model boundary.
bool HasModcryptAreas
Reports whether either modcrypt interval has content, independently from key-selection flags.
uint TotalImageSize
Reports the DSi metadata's total content extent, distinct from physical padding and the common used-s...
uint PrivateSaveSize
Declares private writable storage bytes requested by DSi software, not an embedded ROM region.
uint Arm7DeviceListAddress
Identifies the runtime ARM7 address of the DSi device-list structure used during service initializati...
uint RegionFlags
Gets permitted DSi region flags.
bool VerifyRsaSignature(NdsDsiRsaPublicKey publicKey)
Verifies the stored signature against an explicitly trusted public key and the preserved final bytes ...
ReadOnlyMemory< byte > DigestMasterHmac
Contains the 20-byte SHA-1 HMAC authenticating the digest hierarchy's master data.
NdsRegion BlockHashTable
Locates the second-level SHA-1 array covering groups of sector hashes.
ReadOnlyMemory< byte > Arm7Hmac
Contains the 20-byte SHA-1 HMAC authenticating the common ARM7 payload; no key-validity claim is impl...
uint AccessControl
Gets DSi access-control flags.
ReadOnlyMemory< byte > Arm9WithoutSecureAreaHmac
Contains the alternate ARM9 HMAC calculated with its secure-area portion excluded.
ReadOnlyMemory< byte > Arm7iHmac
Contains the 20-byte SHA-1 HMAC authenticating the DSi-mode ARM7i payload.
NdsRegion TwlDigest
Locates the TWL-mode content range covered by the hierarchical DSi digest tables.
ReadOnlyMemory< byte > RawData
Preserves bytes 0x180-0xFFF, including reserved and cryptographic fields not yet interpreted.
byte ApplicationFlags
Gets DSi application flags.
ReadOnlyMemory< byte > DebugArguments
Gets raw DSi debug arguments.
NdsRegion ModcryptArea1
Locates the first optional region transformed by DSi modcrypt when corresponding flags enable it.
ulong TitleId
Combines low and high words into the platform title identifier used by DSi services and save storage.
NdsRegion ModcryptArea2
Locates the second optional region transformed by DSi modcrypt when corresponding flags enable it.
Represents one explicitly trusted RSA-1024 public key for DSi header authentication....
bool VerifyHeader(ReadOnlySpan< byte > signedHeader, ReadOnlySpan< byte > signature)
Verifies the format-mandated RSA-SHA1 PKCS#1 v1.5 signature over a finalized header prefix.
Identifies a bounded range of bytes in an image.
Definition NdsRegion.cs:11
NdsModcryptArea
Identifies which DSi modcrypt interval and HMAC-derived initial counter a transformation uses.
@ First
Selects the first interval and the first sixteen bytes of the ARM9 HMAC.
@ Second
Selects the second interval and the first sixteen bytes of the ARM7 HMAC.