Advertisement
Dragonheart91

Untitled

May 2nd, 2013
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.13 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.  
  11. namespace Radeon_HD_7000_BIOS_Editor
  12. {
  13.     public partial class MainForm : Form
  14.     {
  15.         public MainForm()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.        
  20.         //
  21.         //Variables
  22.         //
  23.  
  24.         #region Variables Initialization
  25.  
  26.         public static string filePath;
  27.         public static byte[] searchStringPL;
  28.         public static byte[] searchStringVolt;
  29.         public static byte[] searchStringVoltNoRef;
  30.         public static int positionPL;
  31.         public static int positionVoltage;
  32.         public static string selectedLimit;
  33.         public static bool referenceCard = true;
  34.         public static int numberOfStrings;
  35.         public static byte[,] voltageStrings;
  36.  
  37.         #endregion
  38.  
  39.         //
  40.         //Opening BIOS
  41.         //
  42.  
  43.         #region Open File Dialog
  44.         private void openBiosItem_Click(object sender, EventArgs e)
  45.         {
  46.             if (openFileDialog1.ShowDialog() == DialogResult.OK)
  47.             {
  48.                 filePath = openFileDialog1.FileName;
  49.                 BiosRW.readToString(filePath);
  50.                 if (comboBoxVga.Text != null)
  51.                 {
  52.                     ReadInfo();
  53.                 }
  54.             }
  55.         }
  56.  
  57.         #endregion
  58.  
  59.         //
  60.         //Search strings updating
  61.         //
  62.  
  63.         #region Search strings for Power Limit
  64.  
  65.         private void comboBoxVga_SelectedIndexChanged(object sender, EventArgs e)
  66.         {
  67.             if (comboBoxVga.Text == "Radeon HD 7700")
  68.             {
  69.                 searchStringPL = new byte[] { 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x04 };
  70.                 searchStringVolt = new byte[] { 0x28, 0x00, 0x03, 0x01 };
  71.                 searchStringVoltNoRef = new byte[] { 0x01, 0x11, 0x36, 0x00, 0x00, 0x07 };
  72.             }
  73.  
  74.             if (comboBoxVga.Text == "Radeon HD 7800")
  75.             {
  76.                 searchStringPL = new byte[] { 0x00, 0x00, 0x14, 0x00, 0x4B, 0x00, 0x04 };
  77.                 searchStringVolt = new byte[] { 0x4C, 0x00, 0x03, 0x01 };
  78.                 searchStringVoltNoRef = new byte[] { 0x04, 0x00, 0x24, 0x00, 0x00, 0x04 };
  79.             }
  80.  
  81.             if (comboBoxVga.Text == "Radeon HD 7900")
  82.             {
  83.                 searchStringPL = new byte[] { 0x00, 0x00, 0x14, 0x00, 0x40, 0x00, 0x04 };
  84.                 searchStringVolt = new byte[] { 0x40, 0x00, 0x03, 0x01 };
  85.                 searchStringVoltNoRef = new byte[] { 0x04, 0x00, 0x24, 0x00, 0x00, 0x04 };
  86.             }
  87.  
  88.             if (filePath != null) ReadInfo();
  89.         }
  90.  
  91.         #endregion
  92.  
  93.         //
  94.         //Reading Power Limit info and Voltage Table
  95.         //
  96.  
  97.         #region Reading all info
  98.  
  99.         public void ReadInfo()
  100.         {
  101.             IndexOfPL(BiosRW.bios, searchStringPL);
  102.             IndexOfVoltage(BiosRW.bios, searchStringVolt);
  103.         }
  104.  
  105.         #endregion
  106.  
  107.         //
  108.         //Searching Positions
  109.         //
  110.  
  111.         #region Searching Power Limit Position in File
  112.  
  113.         public void IndexOfPL(byte[] ByteArrayToSearch, byte[] ByteArrayToFind)
  114.         {
  115.             if (BiosRW.bios != null && searchStringPL != null)
  116.             {
  117.                 ByteArrayToFind[2] = 0x14;
  118.                 for (int i = 0; i < 2; i++)
  119.                 {
  120.                     Encoding encoding = Encoding.GetEncoding(1252);
  121.  
  122.                     string toSearch = encoding.GetString(ByteArrayToSearch, 0, ByteArrayToSearch.Length);
  123.                     string toFind = encoding.GetString(ByteArrayToFind, 0, ByteArrayToFind.Length);
  124.                     positionPL = toSearch.IndexOf(toFind, StringComparison.Ordinal);
  125.                     if (positionPL != -1) break;
  126.                     if (ByteArrayToFind[2] == 0x14) ByteArrayToFind[2] = 0x32;
  127.                 }
  128.                 if (positionPL == -1)
  129.                 {
  130.                     FormResetPowerLimit();
  131.                     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);
  132.                 }
  133.                 else
  134.                 {
  135.                     FormRefreshPowerLimit();
  136.                 }
  137.             }
  138.         }
  139.  
  140.         #endregion
  141.  
  142.         #region Searching Voltage Table Position in File
  143.  
  144.         public void IndexOfVoltage(byte[] ByteArrayToSearch, byte[] ByteArrayToFind)
  145.         {
  146.             if (BiosRW.bios != null && searchStringVolt != null && searchStringVoltNoRef != null)
  147.             {
  148.                     Encoding encoding = Encoding.GetEncoding(1252);
  149.  
  150.                     string toSearch = encoding.GetString(ByteArrayToSearch, 0, ByteArrayToSearch.Length);
  151.                     string toFind = encoding.GetString(ByteArrayToFind, 0, ByteArrayToFind.Length);
  152.                     positionVoltage = toSearch.IndexOf(toFind, StringComparison.Ordinal);
  153.  
  154.                 if (positionVoltage == -1)
  155.                 {
  156.                     FormResetVoltage();
  157.                     MessageBox.Show("Unable to find voltage table.", "Can't Patch!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  158.                 }
  159.                 else
  160.                 {
  161.                     IfReference();
  162.                 }
  163.             }
  164.         }
  165.  
  166.         #endregion
  167.  
  168.         //
  169.         //Checking BIOS
  170.         //
  171.         public void IfReference()
  172.         {
  173.             int newPositionVoltage = positionVoltage;
  174.             int previous = Convert.ToInt32(BiosRW.bios[newPositionVoltage - 1]);
  175.             numberOfStrings = 0;
  176.             while (Convert.ToInt32(BiosRW.bios[newPositionVoltage - 1]) == previous || Convert.ToInt32(BiosRW.bios[newPositionVoltage - 1]) == previous - 1)
  177.             {
  178.                 previous = Convert.ToInt32(BiosRW.bios[newPositionVoltage - 1]);
  179.                 if (BiosRW.bios[newPositionVoltage - 4] == 0x00)
  180.                 {
  181.                     referenceCard = false;
  182.                     break;
  183.                 }
  184.                 newPositionVoltage = newPositionVoltage - 6;
  185.                 numberOfStrings++;
  186.             }
  187.             if (referenceCard == false)
  188.             {
  189.                 labelReference.Text = "Reference Card: No";
  190.                 MessageBox.Show("Your card is non reference, voltage control is impossible.", "Wait for the next version!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  191.             }
  192.             else
  193.             {
  194.                 labelReference.Text = "Reference Card: Yes";
  195.                 ReadVoltageTable();
  196.             }
  197.         }
  198.  
  199.         public void ReadVoltageTable()
  200.         {
  201.            
  202.             voltageStrings = new byte[numberOfStrings,6];
  203.             int newPositionVoltage = positionVoltage - 6*numberOfStrings;
  204.             for (int i = 0; i < numberOfStrings; i++)
  205.             {
  206.                 for (int y = 0; y < 6; y++)
  207.                 {
  208.                     voltageStrings[i,y] = BiosRW.bios[newPositionVoltage + y];
  209.                 }
  210.                 newPositionVoltage = newPositionVoltage + 6;
  211.             }
  212.             ShowVoltageTable();
  213.         }
  214.  
  215.         public void ShowVoltageTable()
  216.         {
  217.             string[] vid = new string[3];
  218.             for (int i = 0; i < numberOfStrings; i++)
  219.             {
  220.                 for (int y = 0; y < 6; y = y + 2)
  221.                 {
  222.                     if (y == 0)
  223.                     {
  224.                         richTextBox1.Text += "VID: ";
  225.                         vid[0] = voltageStrings[i, y + 1].ToString("X");
  226.                         if (vid[0].Length == 1) vid[0] = 0 + vid[0];
  227.                         vid[1] = voltageStrings[i, y].ToString("X");
  228.                         if (vid[1].Length == 1) vid[1] = 0 + vid[1];
  229.                         vid[2] = vid[0] + vid[1];
  230.                         richTextBox1.Text += Convert.ToInt32(vid[2], 16).ToString() + " mV ";
  231.                     }
  232.                     if (y == 2)
  233.                     {
  234.                         richTextBox1.Text += "Signal: ";
  235.                         vid[0] = voltageStrings[i, y + 1].ToString("X");
  236.                         if (vid[0].Length == 1) vid[0] = 0 + vid[0];
  237.                         vid[1] = voltageStrings[i, y].ToString("X");
  238.                         if (vid[1].Length == 1) vid[1] = 0 + vid[1];
  239.                         vid[2] = vid[0] + vid[1];
  240.                         richTextBox1.Text += Convert.ToInt32(vid[2], 16).ToString() + " ";
  241.                     }
  242.                     if (y == 4)
  243.                     {
  244.                         richTextBox1.Text += "ASIC: ";
  245.                         vid[0] = voltageStrings[i, y + 1].ToString("X");
  246.                         if (vid[0].Length == 1) vid[0] = 0 + vid[0];
  247.                         vid[1] = voltageStrings[i, y].ToString("X");
  248.                         if (vid[1].Length == 1) vid[1] = 0 + vid[1];
  249.                         vid[2] = vid[0] + vid[1];
  250.                         richTextBox1.Text += Convert.ToInt32(vid[2], 16).ToString() + "\r\n";
  251.                     }
  252.                 }
  253.             }
  254.         }
  255.  
  256.  
  257.         //
  258.         //Resetting Form Values
  259.         //
  260.  
  261.         #region Resetting Form Power Limit Values
  262.  
  263.         public void FormResetPowerLimit()
  264.         {
  265.             labelPL.Text = "Current Power Limit: N/A";
  266.             btnPatch.Enabled = false;
  267.             radioBtnPl20.Checked = false;
  268.             radioBtnPl50.Checked = false;
  269.         }
  270.  
  271.         #endregion
  272.  
  273.         #region Resetting Form Voltage Values
  274.  
  275.         public void FormResetVoltage()
  276.         {
  277.             labelReference.Text = "Reference Card: N/A";
  278.         }
  279.  
  280.         #endregion
  281.  
  282.         //
  283.         //Refreshing Form Values
  284.         //
  285.  
  286.         #region Selected Power Limit Changes Event
  287.  
  288.         private void radioBtnPl20_CheckedChanged(object sender, EventArgs e)
  289.         {
  290.             if (radioBtnPl20.Checked == true) selectedLimit = "20";
  291.         }
  292.  
  293.         private void radioBtnPl50_CheckedChanged(object sender, EventArgs e)
  294.         {
  295.             if (radioBtnPl50.Checked == true) selectedLimit = "50";
  296.         }
  297.  
  298.         #endregion
  299.  
  300.         #region Refreshing Form Power Limit Values
  301.  
  302.         private void FormRefreshPowerLimit()
  303.         {
  304.             labelPL.Text = "Current Power Limit: ±" + (Convert.ToString(BiosRW.bios[positionPL + 2])) + "%";
  305.             if ((Convert.ToString(BiosRW.bios[positionPL + 2])) == "20")
  306.             {
  307.                 radioBtnPl20.Checked = true;
  308.             }
  309.             if ((Convert.ToString(BiosRW.bios[positionPL + 2])) == "50")
  310.             {
  311.                 radioBtnPl50.Checked = true;
  312.             }
  313.             btnPatch.Enabled = true;
  314.         }
  315.  
  316.         #endregion
  317.  
  318.         #region Refreshing Form Voltage Values
  319.  
  320.         private void FormRefreshVoltage()
  321.         {
  322.            
  323.         }
  324.  
  325.         #endregion
  326.  
  327.         //
  328.         //Patching
  329.         //
  330.  
  331.         #region Patch Button Click
  332.  
  333.         private void btnPatch_Click(object sender, EventArgs e)
  334.         {
  335.             if (BiosRW.bios == null && searchStringPL == null)
  336.             {
  337.                 MessageBox.Show("Select any BIOS file and your VGA model!", "Can't Patch!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  338.             }
  339.  
  340.             if (BiosRW.bios == null && searchStringPL != null)
  341.             {
  342.                 MessageBox.Show("Select any BIOS file!.", "Can't Patch!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  343.             }
  344.  
  345.             if (BiosRW.bios != null && searchStringPL == null)
  346.             {
  347.                 MessageBox.Show("Select your VGA model!", "Can't Patch!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  348.             }
  349.  
  350.             if (selectedLimit != Convert.ToString(BiosRW.bios[positionPL + 2]))
  351.             {
  352.                 BiosRW.editBios();
  353.                 BiosRW.writeToFile();
  354.                 MessageBox.Show("Patched succesfully!", "Congratulations!", MessageBoxButtons.OK, MessageBoxIcon.Information);
  355.             }
  356.             else
  357.             {
  358.                 MessageBox.Show("You haven't selected any changes!", "Nothing to patch!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  359.             }
  360.         }
  361.  
  362.         #endregion
  363.  
  364.         //
  365.         //Opening other forms
  366.         //
  367.  
  368.         #region Opening other Forms and Exit
  369.  
  370.         private void exitItem_Click(object sender, EventArgs e)
  371.         {
  372.             Close();
  373.         }
  374.        
  375.         private void aboutItem_Click(object sender, EventArgs e)
  376.         {
  377.             AboutForm aboutform = new AboutForm();
  378.             aboutform.Owner = this;
  379.             aboutform.ShowDialog();
  380.         }
  381.  
  382.         private void helpItem_Click(object sender, EventArgs e)
  383.         {
  384.             HelpForm helpform = new HelpForm();
  385.             helpform.Owner = this;
  386.             helpform.Show();
  387.         }
  388.  
  389.         #endregion
  390.  
  391.     }
  392. }
  393.  
  394.  
  395. using System;
  396. using System.Collections.Generic;
  397. using System.Linq;
  398. using System.Text;
  399. using System.IO;
  400.  
  401. namespace Radeon_HD_7000_BIOS_Editor
  402. {
  403.     public static class BiosRW
  404.     {
  405.         public static byte[] bios;
  406.         public static byte[] modBios;
  407.  
  408.         public static void readToString(string filePath)
  409.         {
  410.             FileStream fs = new FileStream(filePath, FileMode.Open);
  411.             BinaryReader br = new BinaryReader(fs);
  412.             {
  413.                 bios = br.ReadBytes(Convert.ToInt32(fs.Length));
  414.             }
  415.             br.Close();
  416.             fs.Close();
  417.         }
  418.  
  419.         public static void editBios()
  420.         {
  421.             modBios = bios;
  422.             modBios[MainForm.positionPL + 2] = Convert.ToByte(MainForm.selectedLimit);
  423.         }
  424.  
  425.         public static void writeToFile()
  426.         {
  427.             string newpath = "";
  428.             string[] path = MainForm.filePath.Split('\\');
  429.             for (int i=0; i < path.Length -1; i++)
  430.             {
  431.                 newpath += path[i];
  432.             }
  433.             FileStream fs = new FileStream(newpath + "\\_NewBios.rom", FileMode.Create);
  434.             BinaryWriter bw = new BinaryWriter(fs);
  435.             bw.Write(modBios);
  436.             bw.Close();
  437.             fs.Close();
  438.         }
  439.     }
  440. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement