Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace Radeon_HD_7000_BIOS_Editor
- {
- public partial class MainForm : Form
- {
- public MainForm()
- {
- InitializeComponent();
- }
- //
- //Variables
- //
- #region Variables Initialization
- public static string filePath;
- public static byte[] searchStringPL;
- public static byte[] searchStringVolt;
- public static byte[] searchStringVoltNoRef;
- public static int positionPL;
- public static int positionVoltage;
- public static string selectedLimit;
- public static bool referenceCard = true;
- public static int numberOfStrings;
- public static byte[,] voltageStrings;
- public static string voltageTable;
- public static int currentVoltage;
- public static int currentSignal;
- public static int rightString;
- public static int[] stepsVoltageSignal = new int[2];
- public static double voltagePerStep;
- public static int fullPositionVoltage;
- public static int fullPositionSignal;
- #endregion
- //
- //Opening BIOS
- //
- #region Open File Dialog
- private void openBiosItem_Click(object sender, EventArgs e)
- {
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- filePath = openFileDialog1.FileName;
- BiosRW.readToString(filePath);
- if (comboBoxVga.Text != null)
- {
- ReadInfo();
- }
- }
- }
- #endregion
- //
- //Search strings updating
- //
- #region Search strings for Power Limit
- private void comboBoxVga_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (comboBoxVga.Text == "Radeon HD 7700")
- {
- searchStringPL = new byte[] { 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x04 };
- searchStringVolt = new byte[] { 0x28, 0x00, 0x03, 0x01 };
- searchStringVoltNoRef = new byte[] { 0x01, 0x11, 0x36, 0x00, 0x00, 0x07 };
- }
- if (comboBoxVga.Text == "Radeon HD 7800")
- {
- searchStringPL = new byte[] { 0x00, 0x00, 0x14, 0x00, 0x4B, 0x00, 0x04 };
- searchStringVolt = new byte[] { 0x4C, 0x00, 0x03, 0x01 };
- searchStringVoltNoRef = new byte[] { 0x04, 0x00, 0x24, 0x00, 0x00, 0x04 };
- }
- if (comboBoxVga.Text == "Radeon HD 7900")
- {
- searchStringPL = new byte[] { 0x00, 0x00, 0x14, 0x00, 0x40, 0x00, 0x04 };
- searchStringVolt = new byte[] { 0x40, 0x00, 0x03, 0x01 };
- searchStringVoltNoRef = new byte[] { 0x04, 0x00, 0x24, 0x00, 0x00, 0x04 };
- }
- if (filePath != null) ReadInfo();
- }
- #endregion
- //
- //Reading Power Limit info and Voltage Table
- //
- #region Reading all info
- public void ReadInfo()
- {
- IndexOfPL(BiosRW.bios, searchStringPL);
- IndexOfVoltage(BiosRW.bios, searchStringVolt);
- }
- #endregion
- //
- //Searching Positions
- //
- #region Searching Power Limit Position in File
- public void IndexOfPL(byte[] ByteArrayToSearch, byte[] ByteArrayToFind)
- {
- if (BiosRW.bios != null && searchStringPL != null)
- {
- ByteArrayToFind[2] = 0x14;
- for (int i = 0; i < 2; i++)
- {
- Encoding encoding = Encoding.GetEncoding(1252);
- string toSearch = encoding.GetString(ByteArrayToSearch, 0, ByteArrayToSearch.Length);
- string toFind = encoding.GetString(ByteArrayToFind, 0, ByteArrayToFind.Length);
- positionPL = toSearch.IndexOf(toFind, StringComparison.Ordinal);
- if (positionPL != -1) break;
- if (ByteArrayToFind[2] == 0x14) ByteArrayToFind[2] = 0x32;
- }
- if (positionPL == -1)
- {
- FormResetPowerLimit();
- MessageBox.Show("Can't find Power Limit value! Does current value differ from 20/50%?", "Check if your BIOS and VGA match.", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- else
- {
- FormRefreshPowerLimit();
- }
- }
- }
- #endregion
- #region Searching Voltage Table Position in File
- public void IndexOfVoltage(byte[] ByteArrayToSearch, byte[] ByteArrayToFind)
- {
- if (BiosRW.bios != null && searchStringVolt != null && searchStringVoltNoRef != null)
- {
- Encoding encoding = Encoding.GetEncoding(1252);
- string toSearch = encoding.GetString(ByteArrayToSearch, 0, ByteArrayToSearch.Length);
- string toFind = encoding.GetString(ByteArrayToFind, 0, ByteArrayToFind.Length);
- positionVoltage = toSearch.IndexOf(toFind, StringComparison.Ordinal);
- if (positionVoltage == -1)
- {
- FormResetVoltage();
- MessageBox.Show("Unable to find voltage table.", "Can't Patch!", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- else
- {
- IfReference();
- }
- }
- }
- #endregion
- //
- //Checking BIOS
- //
- public void IfReference()
- {
- int newPositionVoltage = positionVoltage;
- int previous = Convert.ToInt32(BiosRW.bios[newPositionVoltage - 1]);
- numberOfStrings = 0;
- while (Convert.ToInt32(BiosRW.bios[newPositionVoltage - 1]) == previous || Convert.ToInt32(BiosRW.bios[newPositionVoltage - 1]) == previous - 1)
- {
- previous = Convert.ToInt32(BiosRW.bios[newPositionVoltage - 1]);
- if (BiosRW.bios[newPositionVoltage - 4] == 0x00)
- {
- referenceCard = false;
- break;
- }
- newPositionVoltage = newPositionVoltage - 6;
- numberOfStrings++;
- }
- if (referenceCard == false)
- {
- labelReference.Text = "Reference Card: No";
- MessageBox.Show("Your card is non reference, voltage control is impossible.", "Wait for the next version!", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- else
- {
- labelReference.Text = "Reference Card: Yes";
- ReadVoltageTable();
- }
- }
- public void ReadVoltageTable()
- {
- voltageStrings = new byte[numberOfStrings,6];
- int newPositionVoltage = positionVoltage - 6*numberOfStrings;
- for (int i = 0; i < numberOfStrings; i++)
- {
- for (int y = 0; y < 6; y++)
- {
- voltageStrings[i,y] = BiosRW.bios[newPositionVoltage + y];
- }
- newPositionVoltage = newPositionVoltage + 6;
- }
- ShowVoltageTable();
- }
- public void ShowVoltageTable()
- {
- for (int i = 0; i < numberOfStrings; i++)
- {
- for (int y = 0; y < 6; y = y + 2)
- {
- if (y == 0)
- {
- voltageTable += "VID: ";
- voltageTable += bigEndianToLittle(voltageStrings[i, y], voltageStrings[i, y + 1]) + " mV ";
- }
- if (y == 2)
- {
- voltageTable += "Signal: ";
- voltageTable += bigEndianToLittle(voltageStrings[i, y], voltageStrings[i, y + 1]) + " ";
- }
- if (y == 4)
- {
- voltageTable += "ASIC: ";
- voltageTable += bigEndianToLittle(voltageStrings[i, y], voltageStrings[i, y + 1]) + " %\r\n";
- }
- }
- }
- }
- //
- //Resetting Form Values
- //
- #region Resetting Form Power Limit Values
- public void FormResetPowerLimit()
- {
- labelPL.Text = "Current Power Limit: N/A";
- btnPatch.Enabled = false;
- radioBtnPl20.Checked = false;
- radioBtnPl50.Checked = false;
- }
- #endregion
- #region Resetting Form Voltage Values
- public void FormResetVoltage()
- {
- labelReference.Text = "Reference Card: N/A";
- }
- #endregion
- //
- //Refreshing Form Values
- //
- #region Selected Power Limit Changes Event
- private void radioBtnPl20_CheckedChanged(object sender, EventArgs e)
- {
- if (radioBtnPl20.Checked == true) selectedLimit = "20";
- }
- private void radioBtnPl50_CheckedChanged(object sender, EventArgs e)
- {
- if (radioBtnPl50.Checked == true) selectedLimit = "50";
- }
- #endregion
- #region Refreshing Form Power Limit Values
- private void FormRefreshPowerLimit()
- {
- labelPL.Text = "Current Power Limit: ±" + (Convert.ToString(BiosRW.bios[positionPL + 2])) + "%";
- if ((Convert.ToString(BiosRW.bios[positionPL + 2])) == "20")
- {
- radioBtnPl20.Checked = true;
- }
- if ((Convert.ToString(BiosRW.bios[positionPL + 2])) == "50")
- {
- radioBtnPl50.Checked = true;
- }
- btnPatch.Enabled = true;
- }
- #endregion
- #region Refreshing Form Voltage Values
- private void FormRefreshVoltage()
- {
- }
- #endregion
- //
- //Patching
- //
- #region Patch Button Click
- private void btnPatch_Click(object sender, EventArgs e)
- {
- if (BiosRW.bios == null && searchStringPL == null)
- {
- MessageBox.Show("Select any BIOS file and your VGA model!", "Can't Patch!", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- if (BiosRW.bios == null && searchStringPL != null)
- {
- MessageBox.Show("Select any BIOS file!.", "Can't Patch!", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- if (BiosRW.bios != null && searchStringPL == null)
- {
- MessageBox.Show("Select your VGA model!", "Can't Patch!", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- if (selectedLimit != Convert.ToString(BiosRW.bios[positionPL + 2]) || numericUpDownSignal.Enabled == true && currentSignal != numericUpDownSignal.Value)
- {
- BiosRW.editPL();
- if (referenceCard == true && numericUpDownSignal.Enabled == true && currentSignal != numericUpDownSignal.Value)
- {
- fullPositionVoltage = positionVoltage - 6 * (numberOfStrings - rightString);
- fullPositionSignal = positionVoltage - 4 * (numberOfStrings - rightString);
- int Voltage;
- if (checkBoxPatchVID.Checked == false)
- {
- Voltage = currentVoltage;
- }
- else
- {
- Voltage = recalcVoltage();
- }
- BiosRW.editVoltage(fullPositionVoltage, fullPositionSignal, Convert.ToInt32(numericUpDownSignal.Value), Voltage);
- }
- BiosRW.writeToFile();
- MessageBox.Show("Patched succesfully!", "Congratulations!", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- else
- {
- MessageBox.Show("You haven't selected any changes!", "Nothing to patch!", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- #endregion
- //
- //Opening other forms
- //
- #region Opening other Forms and Exit
- private void exitItem_Click(object sender, EventArgs e)
- {
- Close();
- }
- private void aboutItem_Click(object sender, EventArgs e)
- {
- AboutForm aboutform = new AboutForm();
- aboutform.Owner = this;
- aboutform.ShowDialog();
- }
- private void helpItem_Click(object sender, EventArgs e)
- {
- HelpForm helpform = new HelpForm();
- helpform.Owner = this;
- helpform.Show();
- }
- #endregion
- private void btnConfirmAsic_Click(object sender, EventArgs e)
- {
- string[] asictext;
- int asicSelected, asicFromString, previous = 0;
- asictext = maskedTextBox1.Text.Split(',');
- asictext[0] = asictext[0] + asictext[1];
- asicSelected = Convert.ToInt32(asictext[0]);
- for (int i = 0; i < numberOfStrings; i++)
- {
- asicFromString = Convert.ToInt32(bigEndianToLittle(voltageStrings[i, 4], voltageStrings[i, 5]));
- if (i == 0)
- {
- if (asicSelected <= asicFromString)
- {
- currentVoltage = Convert.ToInt32(bigEndianToLittle(voltageStrings[i, 0], voltageStrings[i, 1]));
- currentSignal = Convert.ToInt32(bigEndianToLittle(voltageStrings[i, 2], voltageStrings[i, 3]));
- rightString = i;
- refreshVoltage();
- }
- }
- if (i== 1 || i== 2 || i== 3 || i== 4 )
- {
- if (asicSelected <= asicFromString && asicSelected > previous)
- {
- currentVoltage = Convert.ToInt32(bigEndianToLittle(voltageStrings[i, 0], voltageStrings[i, 1]));
- currentSignal = Convert.ToInt32(bigEndianToLittle(voltageStrings[i, 2], voltageStrings[i, 3]));
- rightString = i;
- refreshVoltage();
- }
- }
- if ( i == 5)
- {
- if (asicSelected > previous)
- {
- currentVoltage = Convert.ToInt32(bigEndianToLittle(voltageStrings[i, 0], voltageStrings[i, 1]));
- currentSignal = Convert.ToInt32(bigEndianToLittle(voltageStrings[i, 2], voltageStrings[i, 3]));
- rightString = i;
- refreshVoltage();
- }
- }
- previous = asicFromString;
- }
- }
- public void refreshVoltage()
- {
- labelVoltage.Text = "Current 3D Voltage: " + Convert.ToString(currentVoltage) + " mV";
- newVoltage.Text = Convert.ToString(currentVoltage);
- numericUpDownSignal.Enabled = true;
- numericUpDownSignal.Value = currentSignal;
- detectStep();
- }
- public void detectStep()
- {
- int[] position = new int[2];
- int[] signalDifference = new int[2];
- for (int i = 0; i < numberOfStrings; i++)
- {
- if (i == 0)
- {
- signalDifference[0] = Convert.ToInt32(bigEndianToLittle(voltageStrings[i, 2], voltageStrings[i, 3])) - Convert.ToInt32(bigEndianToLittle(voltageStrings[i + 1, 2], voltageStrings[i + 1, 3]));
- position[0] = i; position[1] = i + 1;
- }
- else
- {
- signalDifference[1] = Convert.ToInt32(bigEndianToLittle(voltageStrings[i - 1, 2], voltageStrings[i - 1, 3])) - Convert.ToInt32(bigEndianToLittle(voltageStrings[i, 2], voltageStrings[i, 3]));
- }
- if (signalDifference[1] > signalDifference[0])
- {
- signalDifference[0] = signalDifference[1];
- position[0] = i; position[1] = i + 1;
- }
- }
- stepsVoltageSignal[0] = signalDifference[0];
- stepsVoltageSignal[1] = Convert.ToInt32(bigEndianToLittle(voltageStrings[position[0], 0], voltageStrings[position[0], 1])) - Convert.ToInt32(bigEndianToLittle(voltageStrings[position[1], 0], voltageStrings[position[1], 1]));
- voltagePerStep = Convert.ToDouble(stepsVoltageSignal[1]) / Convert.ToDouble(stepsVoltageSignal[0]);
- }
- public string bigEndianToLittle(byte first, byte second)
- {
- string[] reversedToString = new string[3];
- reversedToString[1] = first.ToString("X");
- if (reversedToString[1].Length == 1) reversedToString[1] = 0 + reversedToString[1];
- reversedToString[2] = second.ToString("X");
- if (reversedToString[2].Length == 1) reversedToString[2] = 0 + reversedToString[2];
- reversedToString[0] = reversedToString[2] + reversedToString[1];
- return Convert.ToInt32(reversedToString[0], 16).ToString();
- }
- private void numericUpDownSignal_ValueChanged(object sender, EventArgs e)
- {
- newVoltage.Text = Convert.ToString(recalcVoltage());
- }
- public int recalcVoltage()
- {
- int diff, newVolt;
- double addVolt;
- diff = Convert.ToInt32(numericUpDownSignal.Value) - currentSignal;
- addVolt = voltagePerStep * diff;
- newVolt = currentVoltage + Convert.ToInt32(addVolt);
- return newVolt;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace Radeon_HD_7000_BIOS_Editor
- {
- public static class BiosRW
- {
- public static byte[] bios;
- public static byte[] modBios;
- public static void readToString(string filePath)
- {
- FileStream fs = new FileStream(filePath, FileMode.Open);
- BinaryReader br = new BinaryReader(fs);
- {
- bios = br.ReadBytes(Convert.ToInt32(fs.Length));
- }
- br.Close();
- fs.Close();
- }
- public static void editPL()
- {
- modBios = bios;
- modBios[MainForm.positionPL + 2] = Convert.ToByte(MainForm.selectedLimit);
- }
- public static void editVoltage(int positionVoltage, int positionSignal, int Signal, int Voltage)
- {
- modBios[positionSignal] = Convert.ToByte(Convert.ToString(Signal));
- string Volt = Voltage.ToString("X");
- if (Volt.Length == 3) Volt = "0" + Volt;
- string[] temp = new string[2];
- temp[0] = Volt.Substring(0,2);
- temp[1] = Volt.Substring(2,2);
- int[] tempx = new int[2];
- tempx[0] = Convert.ToInt32(temp[0], 16);
- tempx[1] = Convert.ToInt32(temp[1], 16);
- modBios[positionVoltage] = Convert.ToByte(Convert.ToString(tempx[1]));
- modBios[positionVoltage + 1] = Convert.ToByte(Convert.ToString(tempx[0]));
- }
- public static void writeToFile()
- {
- string newpath = "";
- string[] path = MainForm.filePath.Split('\\');
- for (int i=0; i < path.Length -1; i++)
- {
- newpath += path[i];
- }
- FileStream fs = new FileStream(newpath + "\\_NewBios.rom", FileMode.Create);
- BinaryWriter bw = new BinaryWriter(fs);
- bw.Write(modBios);
- bw.Close();
- fs.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement