Add a save option to parse the header of flashcart firmware

This commit is contained in:
fangrong 2024-08-01 13:26:13 +08:00
parent a39ebedf66
commit ed336a23b6
10 changed files with 56 additions and 15 deletions

View File

@ -206,7 +206,8 @@ namespace DSDecmp.Formats
{ {
if (readBytes >= compressedSize) if (readBytes >= compressedSize)
throw new NotEnoughDataException(currentOutSize, decompressedLength); throw new NotEnoughDataException(currentOutSize, decompressedLength);
flags = buffer[buffer.Length - 1 - readBytes]; readBytes++; flags = buffer[buffer.Length - 1 - readBytes];
readBytes++;
mask = 0x80; mask = 0x80;
} }
else else
@ -225,8 +226,15 @@ namespace DSDecmp.Formats
{ {
throw new NotEnoughDataException(currentOutSize, decompressedLength); throw new NotEnoughDataException(currentOutSize, decompressedLength);
} }
int byte1 = buffer[compressedSize - 1 - readBytes]; readBytes++; int byte1 = buffer[compressedSize - 1 - readBytes];
int byte2 = buffer[compressedSize - 1 - readBytes]; readBytes++; readBytes++;
if (readBytes == compressedSize)
{
throw new NotEnoughDataException(currentOutSize, decompressedLength);
}
int byte2 = buffer[compressedSize - 1 - readBytes];
readBytes++;
// the number of bytes to copy // the number of bytes to copy
int length = byte1 >> 4; int length = byte1 >> 4;
@ -261,7 +269,8 @@ namespace DSDecmp.Formats
{ {
if (readBytes >= inLength) if (readBytes >= inLength)
throw new NotEnoughDataException(currentOutSize, decompressedLength); 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; outbuffer[outbuffer.Length - 1 - currentOutSize] = next;
currentOutSize++; currentOutSize++;

View File

@ -34,6 +34,7 @@
this.btn_Cancel = new System.Windows.Forms.Button(); this.btn_Cancel = new System.Windows.Forms.Button();
this.checkBox3 = new System.Windows.Forms.CheckBox(); this.checkBox3 = new System.Windows.Forms.CheckBox();
this.checkBox4 = new System.Windows.Forms.CheckBox(); this.checkBox4 = new System.Windows.Forms.CheckBox();
this.checkBox5 = new System.Windows.Forms.CheckBox();
this.SuspendLayout(); this.SuspendLayout();
// //
// checkBox1 // checkBox1
@ -60,7 +61,7 @@
// //
this.btn_OK.DialogResult = System.Windows.Forms.DialogResult.OK; this.btn_OK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btn_OK.Image = global::Tinke.Properties.Resources.accept; this.btn_OK.Image = global::Tinke.Properties.Resources.accept;
this.btn_OK.Location = new System.Drawing.Point(12, 112); this.btn_OK.Location = new System.Drawing.Point(12, 139);
this.btn_OK.Name = "btn_OK"; this.btn_OK.Name = "btn_OK";
this.btn_OK.Size = new System.Drawing.Size(90, 30); this.btn_OK.Size = new System.Drawing.Size(90, 30);
this.btn_OK.TabIndex = 3; this.btn_OK.TabIndex = 3;
@ -73,7 +74,7 @@
// //
this.btn_Cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.btn_Cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btn_Cancel.Image = global::Tinke.Properties.Resources.cancel; this.btn_Cancel.Image = global::Tinke.Properties.Resources.cancel;
this.btn_Cancel.Location = new System.Drawing.Point(137, 112); this.btn_Cancel.Location = new System.Drawing.Point(137, 139);
this.btn_Cancel.Name = "btn_Cancel"; this.btn_Cancel.Name = "btn_Cancel";
this.btn_Cancel.Size = new System.Drawing.Size(90, 30); this.btn_Cancel.Size = new System.Drawing.Size(90, 30);
this.btn_Cancel.TabIndex = 4; this.btn_Cancel.TabIndex = 4;
@ -104,10 +105,21 @@
this.checkBox4.Text = "S21"; this.checkBox4.Text = "S21";
this.checkBox4.UseVisualStyleBackColor = true; this.checkBox4.UseVisualStyleBackColor = true;
// //
// checkBox5
//
this.checkBox5.AutoSize = true;
this.checkBox5.Location = new System.Drawing.Point(12, 112);
this.checkBox5.Name = "checkBox5";
this.checkBox5.Size = new System.Drawing.Size(53, 19);
this.checkBox5.TabIndex = 7;
this.checkBox5.Text = "S22";
this.checkBox5.UseVisualStyleBackColor = true;
//
// SaveOptions // SaveOptions
// //
this.BackColor = System.Drawing.SystemColors.GradientInactiveCaption; this.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
this.ClientSize = new System.Drawing.Size(239, 154); this.ClientSize = new System.Drawing.Size(239, 181);
this.Controls.Add(this.checkBox5);
this.Controls.Add(this.checkBox4); this.Controls.Add(this.checkBox4);
this.Controls.Add(this.checkBox3); this.Controls.Add(this.checkBox3);
this.Controls.Add(this.btn_Cancel); this.Controls.Add(this.btn_Cancel);
@ -135,5 +147,6 @@
private System.Windows.Forms.Button btn_Cancel; private System.Windows.Forms.Button btn_Cancel;
private System.Windows.Forms.CheckBox checkBox3; private System.Windows.Forms.CheckBox checkBox3;
private System.Windows.Forms.CheckBox checkBox4; private System.Windows.Forms.CheckBox checkBox4;
private System.Windows.Forms.CheckBox checkBox5;
} }
} }

View File

@ -24,6 +24,7 @@ namespace Tinke.Dialog
checkBox2.Text = xml.Element("S1F").Value; checkBox2.Text = xml.Element("S1F").Value;
checkBox3.Text = xml.Element("S20").Value; checkBox3.Text = xml.Element("S20").Value;
checkBox4.Text = xml.Element("S21").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"); } catch { throw new NotImplementedException("There was an error reading the language file"); }
} }
@ -48,6 +49,11 @@ namespace Tinke.Dialog
get { return checkBox4.Checked; } get { return checkBox4.Checked; }
} }
public bool IsFlashCartFirmware
{
get { return checkBox5.Checked; }
}
private void btn_OK_Click(object sender, EventArgs e) private void btn_OK_Click(object sender, EventArgs e)
{ {
this.Close(); this.Close();

View File

@ -1501,6 +1501,7 @@ namespace Tinke
bool keep_original = false; bool keep_original = false;
bool a9_recomp = false; bool a9_recomp = false;
bool a9_bestcomp = false; bool a9_bestcomp = false;
bool bIsFlashCartFW = false;
Nitro.Estructuras.ROMHeader header = romInfo.Cabecera; Nitro.Estructuras.ROMHeader header = romInfo.Cabecera;
Dialog.SaveOptions dialog = new Dialog.SaveOptions(); Dialog.SaveOptions dialog = new Dialog.SaveOptions();
@ -1514,6 +1515,8 @@ namespace Tinke
a9_recomp = true; a9_recomp = true;
if (dialog.IsBetterCompress) if (dialog.IsBetterCompress)
a9_bestcomp = true; a9_bestcomp = true;
if (dialog.IsFlashCartFirmware)
bIsFlashCartFW = true;
Thread create = new Thread(ThreadEspera) Thread create = new Thread(ThreadEspera)
{ {
@ -1588,7 +1591,7 @@ namespace Tinke
} }
// Calc Secure Area CRC // 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); Array.Copy(arm9Data, 0x800, this.secureArea.EncryptedData, 0x800, 0x3800);
header.secureCRC16 = SecureArea.CalcCRC(this.secureArea.EncryptedData, gameCode); header.secureCRC16 = SecureArea.CalcCRC(this.secureArea.EncryptedData, gameCode);
@ -1792,7 +1795,8 @@ namespace Tinke
Nitro.FAT.Write(fileFAT, accion.Root, header.FAToffset, accion.SortedIDs, arm9overlayOffset, arm7overlayOffset, header); Nitro.FAT.Write(fileFAT, accion.Root, header.FAToffset, accion.SortedIDs, arm9overlayOffset, arm7overlayOffset, header);
currPos += (uint)new FileInfo(fileFAT).Length; currPos += (uint)new FileInfo(fileFAT).Length;
header.bannerOffset = currPos; if (!bIsFlashCartFW)
header.bannerOffset = currPos;
currPos += (uint)new FileInfo(banner).Length; currPos += (uint)new FileInfo(banner).Length;
// Escribimos los archivos // Escribimos los archivos
@ -1801,7 +1805,8 @@ namespace Tinke
currPos += (uint)new FileInfo(files).Length; currPos += (uint)new FileInfo(files).Length;
// Update the ROM size values of the header // Update the ROM size values of the header
header.ROMsize = currPos; if (!bIsFlashCartFW)
header.ROMsize = currPos;
// Update DSi staff header info // Update DSi staff header info
if (this.twl != null && (header.unitCode & 2) > 0) if (this.twl != null && (header.unitCode & 2) > 0)
@ -1898,10 +1903,13 @@ namespace Tinke
hmac.Dispose(); hmac.Dispose();
} }
header.tamaño = (uint)Math.Ceiling(Math.Log(currPos, 2)); if (!bIsFlashCartFW)
// 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.Ceiling(Math.Log(currPos, 2));
header.tamaño = (uint)Math.Pow(2, header.tamaño); // 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 // Get Header CRC
string tempHeader = Path.GetTempFileName(); string tempHeader = Path.GetTempFileName();

View File

@ -35,7 +35,7 @@ namespace Tinke.Tools
hdrptr = hdr.ARM9ramAddress + hdr.ARM9size; hdrptr = hdr.ARM9ramAddress + hdr.ARM9size;
} }
uint postSize = (uint)arm9Data.Length - (hdrptr - hdr.ARM9ramAddress); 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) if (cmparm9)
{ {
Stream input = new MemoryStream(arm9Data); Stream input = new MemoryStream(arm9Data);

View File

@ -427,6 +427,7 @@
<S1F>Keep Original RSA SHA1 Signature</S1F> <S1F>Keep Original RSA SHA1 Signature</S1F>
<S20>Recompress ARM9 binary (BLZ)</S20> <S20>Recompress ARM9 binary (BLZ)</S20>
<S21>Better compress method (BLZ-Cue)</S21> <S21>Better compress method (BLZ-Cue)</S21>
<S22>Is FlashCart Firmware</S22>
</Dialog> </Dialog>
<VisorHex> <VisorHex>
<S00>File</S00> <S00>File</S00>

View File

@ -431,6 +431,7 @@
<S1F>Keep Original RSA SHA1 Signature</S1F> <S1F>Keep Original RSA SHA1 Signature</S1F>
<S20>Recompress ARM9 binary (BLZ)</S20> <S20>Recompress ARM9 binary (BLZ)</S20>
<S21>Better compress method (BLZ-Cue)</S21> <S21>Better compress method (BLZ-Cue)</S21>
<S22>Is FlashCart Firmware</S22>
</Dialog> </Dialog>
<VisorHex> <VisorHex>
<S00>Archivo</S00> <S00>Archivo</S00>

View File

@ -427,6 +427,7 @@
<S1F>Keep Original RSA SHA1 Signature</S1F> <S1F>Keep Original RSA SHA1 Signature</S1F>
<S20>Recompress ARM9 binary (BLZ)</S20> <S20>Recompress ARM9 binary (BLZ)</S20>
<S21>Better compress method (BLZ-Cue)</S21> <S21>Better compress method (BLZ-Cue)</S21>
<S22>Is FlashCart Firmware</S22>
</Dialog> </Dialog>
<VisorHex> <VisorHex>
<S00>Archive</S00> <S00>Archive</S00>

View File

@ -426,6 +426,7 @@
<S1F>Keep Original RSA SHA1 Signature</S1F> <S1F>Keep Original RSA SHA1 Signature</S1F>
<S20>Recompress ARM9 binary (BLZ)</S20> <S20>Recompress ARM9 binary (BLZ)</S20>
<S21>Better compress method (BLZ-Cue)</S21> <S21>Better compress method (BLZ-Cue)</S21>
<S22>Is FlashCart Firmware</S22>
</Dialog> </Dialog>
<VisorHex> <VisorHex>
<S00>File</S00> <S00>File</S00>

View File

@ -424,6 +424,7 @@
<S1F>保持原始的 RSA SHA1 签名</S1F> <S1F>保持原始的 RSA SHA1 签名</S1F>
<S20>重压缩 ARM9 文件 (BLZ 算法)</S20> <S20>重压缩 ARM9 文件 (BLZ 算法)</S20>
<S21>更好的压缩率算法 (BLZ-Cue)</S21> <S21>更好的压缩率算法 (BLZ-Cue)</S21>
<S22>处理烧录卡固件</S22>
</Dialog> </Dialog>
<VisorHex> <VisorHex>
<S00>文件</S00> <S00>文件</S00>