From ed336a23b62c5212ce68972e4b57c671b909f44b Mon Sep 17 00:00:00 2001 From: fangrong Date: Thu, 1 Aug 2024 13:26:13 +0800 Subject: [PATCH] Add a save option to parse the header of flashcart firmware --- Plugins/DSDecmp/DSDecmp/Formats/LZOvl.cs | 17 +++++++++++++---- Tinke/Dialog/SaveOptions.Designer.cs | 19 ++++++++++++++++--- Tinke/Dialog/SaveOptions.cs | 6 ++++++ Tinke/Sistema.cs | 22 +++++++++++++++------- Tinke/Tools/ARM9BLZ.cs | 2 +- Tinke/langs/en-us.xml | 1 + Tinke/langs/es-es.xml | 1 + Tinke/langs/fr-fr.xml | 1 + Tinke/langs/it-it.xml | 1 + Tinke/langs/zh-hans.xml | 1 + 10 files changed, 56 insertions(+), 15 deletions(-) diff --git a/Plugins/DSDecmp/DSDecmp/Formats/LZOvl.cs b/Plugins/DSDecmp/DSDecmp/Formats/LZOvl.cs index 688a746f..4e369624 100644 --- a/Plugins/DSDecmp/DSDecmp/Formats/LZOvl.cs +++ b/Plugins/DSDecmp/DSDecmp/Formats/LZOvl.cs @@ -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 @@ -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; @@ -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++; diff --git a/Tinke/Dialog/SaveOptions.Designer.cs b/Tinke/Dialog/SaveOptions.Designer.cs index d38bfe0a..a333b7ff 100644 --- a/Tinke/Dialog/SaveOptions.Designer.cs +++ b/Tinke/Dialog/SaveOptions.Designer.cs @@ -34,6 +34,7 @@ private void InitializeComponent() this.btn_Cancel = new System.Windows.Forms.Button(); this.checkBox3 = new System.Windows.Forms.CheckBox(); this.checkBox4 = new System.Windows.Forms.CheckBox(); + this.checkBox5 = new System.Windows.Forms.CheckBox(); this.SuspendLayout(); // // checkBox1 @@ -60,7 +61,7 @@ private void InitializeComponent() // this.btn_OK.DialogResult = System.Windows.Forms.DialogResult.OK; 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.Size = new System.Drawing.Size(90, 30); this.btn_OK.TabIndex = 3; @@ -73,7 +74,7 @@ private void InitializeComponent() // this.btn_Cancel.DialogResult = System.Windows.Forms.DialogResult.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.Size = new System.Drawing.Size(90, 30); this.btn_Cancel.TabIndex = 4; @@ -104,10 +105,21 @@ private void InitializeComponent() this.checkBox4.Text = "S21"; 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 // 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.checkBox3); this.Controls.Add(this.btn_Cancel); @@ -135,5 +147,6 @@ private void InitializeComponent() private System.Windows.Forms.Button btn_Cancel; private System.Windows.Forms.CheckBox checkBox3; private System.Windows.Forms.CheckBox checkBox4; + private System.Windows.Forms.CheckBox checkBox5; } } diff --git a/Tinke/Dialog/SaveOptions.cs b/Tinke/Dialog/SaveOptions.cs index d46ca67d..56ecf0b4 100644 --- a/Tinke/Dialog/SaveOptions.cs +++ b/Tinke/Dialog/SaveOptions.cs @@ -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"); } } @@ -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(); diff --git a/Tinke/Sistema.cs b/Tinke/Sistema.cs index 0c06db4c..6a7d4277 100644 --- a/Tinke/Sistema.cs +++ b/Tinke/Sistema.cs @@ -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(); @@ -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) { @@ -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); @@ -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 @@ -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) @@ -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(); diff --git a/Tinke/Tools/ARM9BLZ.cs b/Tinke/Tools/ARM9BLZ.cs index 6fb2eed5..6253b0c8 100644 --- a/Tinke/Tools/ARM9BLZ.cs +++ b/Tinke/Tools/ARM9BLZ.cs @@ -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); diff --git a/Tinke/langs/en-us.xml b/Tinke/langs/en-us.xml index 2fccd251..d271ffa0 100644 --- a/Tinke/langs/en-us.xml +++ b/Tinke/langs/en-us.xml @@ -427,6 +427,7 @@ Keep Original RSA SHA1 Signature Recompress ARM9 binary (BLZ) Better compress method (BLZ-Cue) + Is FlashCart Firmware File diff --git a/Tinke/langs/es-es.xml b/Tinke/langs/es-es.xml index 8e883002..7c383139 100644 --- a/Tinke/langs/es-es.xml +++ b/Tinke/langs/es-es.xml @@ -431,6 +431,7 @@ Keep Original RSA SHA1 Signature Recompress ARM9 binary (BLZ) Better compress method (BLZ-Cue) + Is FlashCart Firmware Archivo diff --git a/Tinke/langs/fr-fr.xml b/Tinke/langs/fr-fr.xml index c4496857..7587d76d 100644 --- a/Tinke/langs/fr-fr.xml +++ b/Tinke/langs/fr-fr.xml @@ -427,6 +427,7 @@ Keep Original RSA SHA1 Signature Recompress ARM9 binary (BLZ) Better compress method (BLZ-Cue) + Is FlashCart Firmware Archive diff --git a/Tinke/langs/it-it.xml b/Tinke/langs/it-it.xml index b591a43e..7a9b5091 100644 --- a/Tinke/langs/it-it.xml +++ b/Tinke/langs/it-it.xml @@ -426,6 +426,7 @@ Keep Original RSA SHA1 Signature Recompress ARM9 binary (BLZ) Better compress method (BLZ-Cue) + Is FlashCart Firmware File diff --git a/Tinke/langs/zh-hans.xml b/Tinke/langs/zh-hans.xml index 0e79d242..e7cc24c5 100644 --- a/Tinke/langs/zh-hans.xml +++ b/Tinke/langs/zh-hans.xml @@ -424,6 +424,7 @@ 保持原始的 RSA SHA1 签名 重压缩 ARM9 文件 (BLZ 算法) 更好的压缩率算法 (BLZ-Cue) + 处理烧录卡固件 文件