Advertisement
Dragonheart91

radeon hd 7000 bios editor v0.4

May 12th, 2013
2,694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 20.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace Radeon_HD_7000_BIOS_Editor
  11. {
  12.     public partial class MainForm : Form
  13.     {
  14.         public MainForm()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.        
  19.         //
  20.         //Variables
  21.         //
  22.  
  23.         #region Variables Initialization
  24.  
  25.         public static string filePath;
  26.         public static byte[] searchStringPL;
  27.         public static byte[] searchStringVolt;
  28.         public static byte[] searchStringVoltNoRef;
  29.         public static int positionPL;
  30.         public static int positionVoltage;
  31.         public static string selectedLimit;
  32.         public static bool referenceCard = true;
  33.         public static int numberOfStrings;
  34.         public static byte[,] voltageStrings;
  35.         public static string voltageTable;
  36.         public static int currentVoltage;
  37.         public static int currentSignal;
  38.         public static int rightString;
  39.         public static int[] stepsVoltageSignal = new int[2];
  40.         public static double voltagePerStep;
  41.         public static int fullPositionVoltage;
  42.         public static int fullPositionSignal;
  43.  
  44.         #endregion
  45.  
  46.         //
  47.         //Opening BIOS
  48.         //
  49.  
  50.         #region Open File Dialog
  51.         private void openBiosItem_Click(object sender, EventArgs e)
  52.         {
  53.             if (openFileDialog1.ShowDialog() == DialogResult.OK)
  54.             {
  55.                 filePath = openFileDialog1.FileName;
  56.                 BiosRW.readToString(filePath);
  57.                 if (comboBoxVga.Text != null)
  58.                 {
  59.                     ReadInfo();
  60.                 }
  61.             }
  62.         }
  63.  
  64.         #endregion
  65.  
  66.         //
  67.         //Search strings updating
  68.         //
  69.  
  70.         #region Search strings for Power Limit
  71.  
  72.         private void comboBoxVga_SelectedIndexChanged(object sender, EventArgs e)
  73.         {
  74.             if (comboBoxVga.Text == "Radeon HD 7700")
  75.             {
  76.                 searchStringPL = new byte[] { 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x04 };
  77.                 searchStringVolt = new byte[] { 0x28, 0x00, 0x03, 0x01 };
  78.                 searchStringVoltNoRef = new byte[] { 0x01, 0x11, 0x36, 0x00, 0x00, 0x07 };
  79.             }
  80.  
  81.             if (comboBoxVga.Text == "Radeon HD 7800")
  82.             {
  83.                 searchStringPL = new byte[] { 0x00, 0x00, 0x14, 0x00, 0x4B, 0x00, 0x04 };
  84.                 searchStringVolt = new byte[] { 0x4C, 0x00, 0x03, 0x01 };
  85.                 searchStringVoltNoRef = new byte[] { 0x04, 0x00, 0x24, 0x00, 0x00, 0x04 };
  86.             }
  87.  
  88.             if (comboBoxVga.Text == "Radeon HD 7900")
  89.             {
  90.                 searchStringPL = new byte[] { 0x00, 0x00, 0x14, 0x00, 0x40, 0x00, 0x04 };
  91.                 searchStringVolt = new byte[] { 0x40, 0x00, 0x03, 0x01 };
  92.                 searchStringVoltNoRef = new byte[] { 0x04, 0x00, 0x24, 0x00, 0x00, 0x04 };
  93.             }
  94.  
  95.             if (filePath != null) ReadInfo();
  96.         }
  97.  
  98.         #endregion
  99.  
  100.         //
  101.         //Reading Power Limit info and Voltage Table
  102.         //
  103.  
  104.         #region Reading all info
  105.  
  106.         public void ReadInfo()
  107.         {
  108.             IndexOfPL(BiosRW.bios, searchStringPL);
  109.             IndexOfVoltage(BiosRW.bios, searchStringVolt);
  110.         }
  111.  
  112.         #endregion
  113.  
  114.         //
  115.         //Searching Positions
  116.         //
  117.  
  118.         #region Searching Power Limit Position in File
  119.  
  120.         public void IndexOfPL(byte[] ByteArrayToSearch, byte[] ByteArrayToFind)
  121.         {
  122.             if (BiosRW.bios != null && searchStringPL != null)
  123.             {
  124.                 ByteArrayToFind[2] = 0x14;
  125.                 for (int i = 0; i < 2; i++)
  126.                 {
  127.                     Encoding encoding = Encoding.GetEncoding(1252);
  128.  
  129.                     string toSearch = encoding.GetString(ByteArrayToSearch, 0, ByteArrayToSearch.Length);
  130.                     string toFind = encoding.GetString(ByteArrayToFind, 0, ByteArrayToFind.Length);
  131.                     positionPL = toSearch.IndexOf(toFind, StringComparison.Ordinal);
  132.                     if (positionPL != -1) break;
  133.                     if (ByteArrayToFind[2] == 0x14) ByteArrayToFind[2] = 0x32;
  134.                 }
  135.                 if (positionPL == -1)
  136.                 {
  137.                     FormResetPowerLimit();
  138.                     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);
  139.                 }
  140.                 else
  141.                 {
  142.                     FormRefreshPowerLimit();
  143.                 }
  144.             }
  145.         }
  146.  
  147.         #endregion
  148.  
  149.         #region Searching Voltage Table Position in File
  150.  
  151.         public void IndexOfVoltage(byte[] ByteArrayToSearch, byte[] ByteArrayToFind)
  152.         {
  153.             if (BiosRW.bios != null && searchStringVolt != null && searchStringVoltNoRef != null)
  154.             {
  155.                     Encoding encoding = Encoding.GetEncoding(1252);
  156.  
  157.                     string toSearch = encoding.GetString(ByteArrayToSearch, 0, ByteArrayToSearch.Length);
  158.                     string toFind = encoding.GetString(ByteArrayToFind, 0, ByteArrayToFind.Length);
  159.                     positionVoltage = toSearch.IndexOf(toFind, StringComparison.Ordinal);
  160.  
  161.                 if (positionVoltage == -1)
  162.                 {
  163.                     FormResetVoltage();
  164.                     MessageBox.Show("Unable to find voltage table.", "Can't Patch!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  165.                 }
  166.                 else
  167.                 {
  168.                     IfReference();
  169.                 }
  170.             }
  171.         }
  172.  
  173.         #endregion
  174.  
  175.         //
  176.         //Checking BIOS
  177.         //
  178.         public void IfReference()
  179.         {
  180.             int newPositionVoltage = positionVoltage;
  181.             int previous = Convert.ToInt32(BiosRW.bios[newPositionVoltage - 1]);
  182.             numberOfStrings = 0;
  183.             while (Convert.ToInt32(BiosRW.bios[newPositionVoltage - 1]) == previous || Convert.ToInt32(BiosRW.bios[newPositionVoltage - 1]) == previous - 1)
  184.             {
  185.                 previous = Convert.ToInt32(BiosRW.bios[newPositionVoltage - 1]);
  186.                 if (BiosRW.bios[newPositionVoltage - 4] == 0x00)
  187.                 {
  188.                     referenceCard = false;
  189.                     break;
  190.                 }
  191.                 newPositionVoltage = newPositionVoltage - 6;
  192.                 numberOfStrings++;
  193.             }
  194.             if (referenceCard == false)
  195.             {
  196.                 labelReference.Text = "Reference Card: No";
  197.                 MessageBox.Show("Your card is non reference, voltage control is impossible.", "Wait for the next version!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  198.             }
  199.             else
  200.             {
  201.                 labelReference.Text = "Reference Card: Yes";
  202.                 ReadVoltageTable();
  203.             }
  204.         }
  205.  
  206.         public void ReadVoltageTable()
  207.         {
  208.            
  209.             voltageStrings = new byte[numberOfStrings,6];
  210.             int newPositionVoltage = positionVoltage - 6*numberOfStrings;
  211.             for (int i = 0; i < numberOfStrings; i++)
  212.             {
  213.                 for (int y = 0; y < 6; y++)
  214.                 {
  215.                     voltageStrings[i,y] = BiosRW.bios[newPositionVoltage + y];
  216.                 }
  217.                 newPositionVoltage = newPositionVoltage + 6;
  218.             }
  219.             ShowVoltageTable();
  220.         }
  221.  
  222.         public void ShowVoltageTable()
  223.         {
  224.             for (int i = 0; i < numberOfStrings; i++)
  225.             {
  226.                 for (int y = 0; y < 6; y = y + 2)
  227.                 {
  228.                     if (y == 0)
  229.                     {
  230.                         voltageTable += "VID: ";
  231.                         voltageTable += bigEndianToLittle(voltageStrings[i, y], voltageStrings[i, y + 1]) + " mV ";
  232.                     }
  233.                     if (y == 2)
  234.                     {
  235.                         voltageTable += "Signal: ";
  236.                         voltageTable += bigEndianToLittle(voltageStrings[i, y], voltageStrings[i, y + 1]) + " ";
  237.                     }
  238.                     if (y == 4)
  239.                     {
  240.                         voltageTable += "ASIC: ";
  241.                         voltageTable += bigEndianToLittle(voltageStrings[i, y], voltageStrings[i, y + 1]) + " %\r\n";
  242.                     }
  243.                 }
  244.             }
  245.         }
  246.  
  247.  
  248.         //
  249.         //Resetting Form Values
  250.         //
  251.  
  252.         #region Resetting Form Power Limit Values
  253.  
  254.         public void FormResetPowerLimit()
  255.         {
  256.             labelPL.Text = "Current Power Limit: N/A";
  257.             btnPatch.Enabled = false;
  258.             radioBtnPl20.Checked = false;
  259.             radioBtnPl50.Checked = false;
  260.         }
  261.  
  262.         #endregion
  263.  
  264.         #region Resetting Form Voltage Values
  265.  
  266.         public void FormResetVoltage()
  267.         {
  268.             labelReference.Text = "Reference Card: N/A";
  269.         }
  270.  
  271.         #endregion
  272.  
  273.         //
  274.         //Refreshing Form Values
  275.         //
  276.  
  277.         #region Selected Power Limit Changes Event
  278.  
  279.         private void radioBtnPl20_CheckedChanged(object sender, EventArgs e)
  280.         {
  281.             if (radioBtnPl20.Checked == true) selectedLimit = "20";
  282.         }
  283.  
  284.         private void radioBtnPl50_CheckedChanged(object sender, EventArgs e)
  285.         {
  286.             if (radioBtnPl50.Checked == true) selectedLimit = "50";
  287.         }
  288.  
  289.         #endregion
  290.  
  291.         #region Refreshing Form Power Limit Values
  292.  
  293.         private void FormRefreshPowerLimit()
  294.         {
  295.             labelPL.Text = "Current Power Limit: ±" + (Convert.ToString(BiosRW.bios[positionPL + 2])) + "%";
  296.             if ((Convert.ToString(BiosRW.bios[positionPL + 2])) == "20")
  297.             {
  298.                 radioBtnPl20.Checked = true;
  299.             }
  300.             if ((Convert.ToString(BiosRW.bios[positionPL + 2])) == "50")
  301.             {
  302.                 radioBtnPl50.Checked = true;
  303.             }
  304.             btnPatch.Enabled = true;
  305.         }
  306.  
  307.         #endregion
  308.  
  309.         #region Refreshing Form Voltage Values
  310.  
  311.         private void FormRefreshVoltage()
  312.         {
  313.            
  314.         }
  315.  
  316.         #endregion
  317.  
  318.         //
  319.         //Patching
  320.         //
  321.  
  322.         #region Patch Button Click
  323.  
  324.         private void btnPatch_Click(object sender, EventArgs e)
  325.         {
  326.             if (BiosRW.bios == null && searchStringPL == null)
  327.             {
  328.                 MessageBox.Show("Select any BIOS file and your VGA model!", "Can't Patch!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  329.             }
  330.  
  331.             if (BiosRW.bios == null && searchStringPL != null)
  332.             {
  333.                 MessageBox.Show("Select any BIOS file!.", "Can't Patch!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  334.             }
  335.  
  336.             if (BiosRW.bios != null && searchStringPL == null)
  337.             {
  338.                 MessageBox.Show("Select your VGA model!", "Can't Patch!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  339.             }
  340.  
  341.             if (selectedLimit != Convert.ToString(BiosRW.bios[positionPL + 2]) || numericUpDownSignal.Enabled == true && currentSignal != numericUpDownSignal.Value)
  342.             {
  343.                 BiosRW.editPL();
  344.                 if (referenceCard == true && numericUpDownSignal.Enabled == true && currentSignal != numericUpDownSignal.Value)
  345.                 {
  346.                     fullPositionVoltage = positionVoltage - 6 * (numberOfStrings - rightString);
  347.                     fullPositionSignal = positionVoltage - 4 * (numberOfStrings - rightString);
  348.                     int Voltage;
  349.                     if (checkBoxPatchVID.Checked == false)
  350.                     {
  351.                         Voltage = currentVoltage;
  352.                     }
  353.                     else
  354.                     {
  355.                         Voltage = recalcVoltage();
  356.                     }
  357.                     BiosRW.editVoltage(fullPositionVoltage, fullPositionSignal, Convert.ToInt32(numericUpDownSignal.Value), Voltage);
  358.                 }
  359.                 BiosRW.writeToFile();
  360.                 MessageBox.Show("Patched succesfully!", "Congratulations!", MessageBoxButtons.OK, MessageBoxIcon.Information);
  361.             }
  362.             else
  363.             {
  364.                 MessageBox.Show("You haven't selected any changes!", "Nothing to patch!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  365.             }
  366.         }
  367.  
  368.         #endregion
  369.  
  370.         //
  371.         //Opening other forms
  372.         //
  373.  
  374.         #region Opening other Forms and Exit
  375.  
  376.         private void exitItem_Click(object sender, EventArgs e)
  377.         {
  378.             Close();
  379.         }
  380.        
  381.         private void aboutItem_Click(object sender, EventArgs e)
  382.         {
  383.             AboutForm aboutform = new AboutForm();
  384.             aboutform.Owner = this;
  385.             aboutform.ShowDialog();
  386.         }
  387.  
  388.         private void helpItem_Click(object sender, EventArgs e)
  389.         {
  390.             HelpForm helpform = new HelpForm();
  391.             helpform.Owner = this;
  392.             helpform.Show();
  393.         }
  394.  
  395.         #endregion
  396.  
  397.         private void btnConfirmAsic_Click(object sender, EventArgs e)
  398.         {
  399.             string[] asictext;
  400.             int asicSelected, asicFromString, previous = 0;
  401.  
  402.             asictext = maskedTextBox1.Text.Split(',');
  403.             asictext[0] = asictext[0] + asictext[1];
  404.             asicSelected = Convert.ToInt32(asictext[0]);
  405.  
  406.             for (int i = 0; i < numberOfStrings; i++)
  407.             {
  408.                 asicFromString = Convert.ToInt32(bigEndianToLittle(voltageStrings[i, 4], voltageStrings[i, 5]));
  409.  
  410.                 if (i == 0)
  411.                 {
  412.                     if (asicSelected <= asicFromString)
  413.                     {
  414.                         currentVoltage = Convert.ToInt32(bigEndianToLittle(voltageStrings[i, 0], voltageStrings[i, 1]));
  415.                         currentSignal = Convert.ToInt32(bigEndianToLittle(voltageStrings[i, 2], voltageStrings[i, 3]));
  416.                         rightString = i;
  417.                         refreshVoltage();
  418.                     }
  419.                 }
  420.  
  421.                 if (i== 1 || i== 2 || i== 3 || i== 4 )
  422.                 {
  423.                     if (asicSelected <= asicFromString && asicSelected > previous)
  424.                     {
  425.                         currentVoltage = Convert.ToInt32(bigEndianToLittle(voltageStrings[i, 0], voltageStrings[i, 1]));
  426.                         currentSignal = Convert.ToInt32(bigEndianToLittle(voltageStrings[i, 2], voltageStrings[i, 3]));
  427.                         rightString = i;
  428.                         refreshVoltage();
  429.                     }
  430.                 }
  431.  
  432.                 if ( i == 5)
  433.                 {
  434.                     if (asicSelected > previous)
  435.                     {
  436.                         currentVoltage = Convert.ToInt32(bigEndianToLittle(voltageStrings[i, 0], voltageStrings[i, 1]));
  437.                         currentSignal = Convert.ToInt32(bigEndianToLittle(voltageStrings[i, 2], voltageStrings[i, 3]));
  438.                         rightString = i;
  439.                         refreshVoltage();
  440.                     }
  441.                 }
  442.                     previous = asicFromString;
  443.             }
  444.         }
  445.  
  446.         public void refreshVoltage()
  447.         {
  448.             labelVoltage.Text = "Current 3D Voltage: " + Convert.ToString(currentVoltage) + " mV";
  449.             newVoltage.Text = Convert.ToString(currentVoltage);
  450.             numericUpDownSignal.Enabled = true;
  451.             numericUpDownSignal.Value = currentSignal;
  452.             detectStep();
  453.         }
  454.  
  455.         public void detectStep()
  456.         {
  457.             int[] position = new int[2];
  458.             int[] signalDifference = new int[2];
  459.             for (int i = 0; i < numberOfStrings; i++)
  460.             {
  461.                 if (i == 0)
  462.                 {
  463.                     signalDifference[0] = Convert.ToInt32(bigEndianToLittle(voltageStrings[i, 2], voltageStrings[i, 3])) - Convert.ToInt32(bigEndianToLittle(voltageStrings[i + 1, 2], voltageStrings[i + 1, 3]));
  464.                     position[0] = i; position[1] = i + 1;
  465.                 }
  466.                 else
  467.                 {
  468.                     signalDifference[1] = Convert.ToInt32(bigEndianToLittle(voltageStrings[i - 1, 2], voltageStrings[i - 1, 3])) - Convert.ToInt32(bigEndianToLittle(voltageStrings[i, 2], voltageStrings[i, 3]));
  469.                 }
  470.                 if (signalDifference[1] > signalDifference[0])
  471.                 {
  472.                     signalDifference[0] = signalDifference[1];
  473.                     position[0] = i; position[1] = i + 1;
  474.                 }
  475.             }
  476.  
  477.             stepsVoltageSignal[0] = signalDifference[0];
  478.  
  479.             stepsVoltageSignal[1] = Convert.ToInt32(bigEndianToLittle(voltageStrings[position[0], 0], voltageStrings[position[0], 1])) - Convert.ToInt32(bigEndianToLittle(voltageStrings[position[1], 0], voltageStrings[position[1], 1]));
  480.  
  481.             voltagePerStep = Convert.ToDouble(stepsVoltageSignal[1]) / Convert.ToDouble(stepsVoltageSignal[0]);
  482.         }
  483.  
  484.         public string bigEndianToLittle(byte first, byte second)
  485.         {
  486.             string[] reversedToString = new string[3];
  487.             reversedToString[1] = first.ToString("X");
  488.             if (reversedToString[1].Length == 1) reversedToString[1] = 0 + reversedToString[1];
  489.             reversedToString[2] = second.ToString("X");
  490.             if (reversedToString[2].Length == 1) reversedToString[2] = 0 + reversedToString[2];
  491.             reversedToString[0] = reversedToString[2] + reversedToString[1];
  492.             return Convert.ToInt32(reversedToString[0], 16).ToString();
  493.         }
  494.  
  495.         private void numericUpDownSignal_ValueChanged(object sender, EventArgs e)
  496.         {
  497.                 newVoltage.Text = Convert.ToString(recalcVoltage());
  498.         }
  499.  
  500.         public int recalcVoltage()
  501.         {
  502.             int diff, newVolt;
  503.             double addVolt;
  504.             diff = Convert.ToInt32(numericUpDownSignal.Value) - currentSignal;
  505.             addVolt = voltagePerStep * diff;
  506.             newVolt = currentVoltage + Convert.ToInt32(addVolt);
  507.             return newVolt;
  508.         }
  509.  
  510.  
  511.     }
  512. }
  513.  
  514.  
  515.  
  516. using System;
  517. using System.Collections.Generic;
  518. using System.Linq;
  519. using System.Text;
  520. using System.IO;
  521.  
  522. namespace Radeon_HD_7000_BIOS_Editor
  523. {
  524.     public static class BiosRW
  525.     {
  526.         public static byte[] bios;
  527.         public static byte[] modBios;
  528.  
  529.         public static void readToString(string filePath)
  530.         {
  531.             FileStream fs = new FileStream(filePath, FileMode.Open);
  532.             BinaryReader br = new BinaryReader(fs);
  533.             {
  534.                 bios = br.ReadBytes(Convert.ToInt32(fs.Length));
  535.             }
  536.             br.Close();
  537.             fs.Close();
  538.         }
  539.  
  540.         public static void editPL()
  541.         {
  542.             modBios = bios;
  543.             modBios[MainForm.positionPL + 2] = Convert.ToByte(MainForm.selectedLimit);
  544.         }
  545.  
  546.  
  547.         public static void editVoltage(int positionVoltage, int positionSignal, int Signal, int Voltage)
  548.         {
  549.             modBios[positionSignal] = Convert.ToByte(Convert.ToString(Signal));
  550.             string Volt = Voltage.ToString("X");
  551.             if (Volt.Length == 3) Volt = "0" + Volt;
  552.             string[] temp = new string[2];
  553.             temp[0] = Volt.Substring(0,2);
  554.             temp[1] = Volt.Substring(2,2);
  555.             int[] tempx = new int[2];
  556.             tempx[0] = Convert.ToInt32(temp[0], 16);
  557.             tempx[1] = Convert.ToInt32(temp[1], 16);
  558.             modBios[positionVoltage] = Convert.ToByte(Convert.ToString(tempx[1]));
  559.             modBios[positionVoltage + 1] = Convert.ToByte(Convert.ToString(tempx[0]));
  560.         }
  561.  
  562.         public static void writeToFile()
  563.         {
  564.             string newpath = "";
  565.             string[] path = MainForm.filePath.Split('\\');
  566.             for (int i=0; i < path.Length -1; i++)
  567.             {
  568.                 newpath += path[i];
  569.             }
  570.             FileStream fs = new FileStream(newpath + "\\_NewBios.rom", FileMode.Create);
  571.             BinaryWriter bw = new BinaryWriter(fs);
  572.             bw.Write(modBios);
  573.             bw.Close();
  574.             fs.Close();
  575.         }
  576.     }
  577. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement