Skip to content

Commit

Permalink
Add a save option to parse the header of flashcart firmware
Browse files Browse the repository at this point in the history
  • Loading branch information
R-YaTian committed Aug 1, 2024
1 parent a39ebed commit ed336a2
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 15 deletions.
17 changes: 13 additions & 4 deletions Plugins/DSDecmp/DSDecmp/Formats/LZOvl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ public override long Decompress(System.IO.Stream instream, long inLength, System
{
if (readBytes >= compressedSize)
throw new NotEnoughDataException(currentOutSize, decompressedLength);
flags = buffer[buffer.Length - 1 - readBytes]; readBytes++;
flags = buffer[buffer.Length - 1 - readBytes];
readBytes++;
mask = 0x80;
}
else
Expand All @@ -225,8 +226,15 @@ public override long Decompress(System.IO.Stream instream, long inLength, System
{
throw new NotEnoughDataException(currentOutSize, decompressedLength);
}
int byte1 = buffer[compressedSize - 1 - readBytes]; readBytes++;
int byte2 = buffer[compressedSize - 1 - readBytes]; readBytes++;
int byte1 = buffer[compressedSize - 1 - readBytes];
readBytes++;

if (readBytes == compressedSize)
{
throw new NotEnoughDataException(currentOutSize, decompressedLength);
}
int byte2 = buffer[compressedSize - 1 - readBytes];
readBytes++;

// the number of bytes to copy
int length = byte1 >> 4;
Expand Down Expand Up @@ -261,7 +269,8 @@ public override long Decompress(System.IO.Stream instream, long inLength, System
{
if (readBytes >= inLength)
throw new NotEnoughDataException(currentOutSize, decompressedLength);
byte next = buffer[buffer.Length - 1 - readBytes]; readBytes++;
byte next = buffer[buffer.Length - 1 - readBytes];
readBytes++;

outbuffer[outbuffer.Length - 1 - currentOutSize] = next;
currentOutSize++;
Expand Down
19 changes: 16 additions & 3 deletions Tinke/Dialog/SaveOptions.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Tinke/Dialog/SaveOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ private void ReadLanguage()
checkBox2.Text = xml.Element("S1F").Value;
checkBox3.Text = xml.Element("S20").Value;
checkBox4.Text = xml.Element("S21").Value;
checkBox5.Text = xml.Element("S22").Value;
}
catch { throw new NotImplementedException("There was an error reading the language file"); }
}
Expand All @@ -48,6 +49,11 @@ public bool IsBetterCompress
get { return checkBox4.Checked; }
}

public bool IsFlashCartFirmware
{
get { return checkBox5.Checked; }
}

private void btn_OK_Click(object sender, EventArgs e)
{
this.Close();
Expand Down
22 changes: 15 additions & 7 deletions Tinke/Sistema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,7 @@ private void btnSaveROM_Click(object sender, EventArgs e)
bool keep_original = false;
bool a9_recomp = false;
bool a9_bestcomp = false;
bool bIsFlashCartFW = false;
Nitro.Estructuras.ROMHeader header = romInfo.Cabecera;

Dialog.SaveOptions dialog = new Dialog.SaveOptions();
Expand All @@ -1514,6 +1515,8 @@ private void btnSaveROM_Click(object sender, EventArgs e)
a9_recomp = true;
if (dialog.IsBetterCompress)
a9_bestcomp = true;
if (dialog.IsFlashCartFirmware)
bIsFlashCartFW = true;

Thread create = new Thread(ThreadEspera)
{
Expand Down Expand Up @@ -1588,7 +1591,7 @@ private void btnSaveROM_Click(object sender, EventArgs e)
}

// Calc Secure Area CRC
if (header.ARM9romOffset == 0x4000 && header.ARM9size >= 0x4000)
if (header.ARM9romOffset == 0x4000 && header.ARM9size >= 0x4000 && !bIsFlashCartFW)
{
Array.Copy(arm9Data, 0x800, this.secureArea.EncryptedData, 0x800, 0x3800);
header.secureCRC16 = SecureArea.CalcCRC(this.secureArea.EncryptedData, gameCode);
Expand Down Expand Up @@ -1792,7 +1795,8 @@ private void btnSaveROM_Click(object sender, EventArgs e)
Nitro.FAT.Write(fileFAT, accion.Root, header.FAToffset, accion.SortedIDs, arm9overlayOffset, arm7overlayOffset, header);
currPos += (uint)new FileInfo(fileFAT).Length;

header.bannerOffset = currPos;
if (!bIsFlashCartFW)
header.bannerOffset = currPos;
currPos += (uint)new FileInfo(banner).Length;

// Escribimos los archivos
Expand All @@ -1801,7 +1805,8 @@ private void btnSaveROM_Click(object sender, EventArgs e)
currPos += (uint)new FileInfo(files).Length;

// Update the ROM size values of the header
header.ROMsize = currPos;
if (!bIsFlashCartFW)
header.ROMsize = currPos;

// Update DSi staff header info
if (this.twl != null && (header.unitCode & 2) > 0)
Expand Down Expand Up @@ -1898,10 +1903,13 @@ private void btnSaveROM_Click(object sender, EventArgs e)
hmac.Dispose();
}

header.tamaño = (uint)Math.Ceiling(Math.Log(currPos, 2));
// Ref. to TWL SDK' "Card Manual" for DSi Cartrige ROMs
if ((header.unitCode & 2) > 0 && (header.tid_high & 0xF) == 0 && header.tamaño < 25) header.tamaño = 25;
header.tamaño = (uint)Math.Pow(2, header.tamaño);
if (!bIsFlashCartFW)
{
header.tamaño = (uint)Math.Ceiling(Math.Log(currPos, 2));
// Ref. to TWL SDK' "Card Manual" for DSi Cartrige ROMs
if ((header.unitCode & 2) > 0 && (header.tid_high & 0xF) == 0 && header.tamaño < 25) header.tamaño = 25;
header.tamaño = (uint)Math.Pow(2, header.tamaño);
}

// Get Header CRC
string tempHeader = Path.GetTempFileName();
Expand Down
2 changes: 1 addition & 1 deletion Tinke/Tools/ARM9BLZ.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static uint Decompress(byte[] arm9Data, Estructuras.ROMHeader hdr, out by
hdrptr = hdr.ARM9ramAddress + hdr.ARM9size;
}
uint postSize = (uint)arm9Data.Length - (hdrptr - hdr.ARM9ramAddress);
bool cmparm9 = hdrptr > hdr.ARM9ramAddress && hdrptr + nitrocode_length >= hdr.ARM9ramAddress + arm9Data.Length;
bool cmparm9 = hdrptr > hdr.ARM9ramAddress && hdrptr + nitrocode_length > hdr.ARM9ramAddress + arm9Data.Length;
if (cmparm9)
{
Stream input = new MemoryStream(arm9Data);
Expand Down
1 change: 1 addition & 0 deletions Tinke/langs/en-us.xml
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@
<S1F>Keep Original RSA SHA1 Signature</S1F>
<S20>Recompress ARM9 binary (BLZ)</S20>
<S21>Better compress method (BLZ-Cue)</S21>
<S22>Is FlashCart Firmware</S22>
</Dialog>
<VisorHex>
<S00>File</S00>
Expand Down
1 change: 1 addition & 0 deletions Tinke/langs/es-es.xml
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@
<S1F>Keep Original RSA SHA1 Signature</S1F>
<S20>Recompress ARM9 binary (BLZ)</S20>
<S21>Better compress method (BLZ-Cue)</S21>
<S22>Is FlashCart Firmware</S22>
</Dialog>
<VisorHex>
<S00>Archivo</S00>
Expand Down
1 change: 1 addition & 0 deletions Tinke/langs/fr-fr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@
<S1F>Keep Original RSA SHA1 Signature</S1F>
<S20>Recompress ARM9 binary (BLZ)</S20>
<S21>Better compress method (BLZ-Cue)</S21>
<S22>Is FlashCart Firmware</S22>
</Dialog>
<VisorHex>
<S00>Archive</S00>
Expand Down
1 change: 1 addition & 0 deletions Tinke/langs/it-it.xml
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@
<S1F>Keep Original RSA SHA1 Signature</S1F>
<S20>Recompress ARM9 binary (BLZ)</S20>
<S21>Better compress method (BLZ-Cue)</S21>
<S22>Is FlashCart Firmware</S22>
</Dialog>
<VisorHex>
<S00>File</S00>
Expand Down
1 change: 1 addition & 0 deletions Tinke/langs/zh-hans.xml
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@
<S1F>保持原始的 RSA SHA1 签名</S1F>
<S20>重压缩 ARM9 文件 (BLZ 算法)</S20>
<S21>更好的压缩率算法 (BLZ-Cue)</S21>
<S22>处理烧录卡固件</S22>
</Dialog>
<VisorHex>
<S00>文件</S00>
Expand Down

0 comments on commit ed336a2

Please sign in to comment.