Example #1
0
        internal void LoadFile(Stream fileStream, TextBox txtChecksum)
        {
            if ((fileStream.Length != 524288) && (fileStream.Length != 524288 / 2))
            {
                MessageBox.Show(PolarisBiosEditorResource.WRONG_BIOS_SIZE, PolarisBiosEditorResource.WARNING, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            using (var br = new BinaryReader(fileStream))
            {
                Buffer = br.ReadBytes((int)fileStream.Length);

                atom_rom_header_offset = GetValueAtPosition(16, atom_rom_header_ptr);
                atom_rom_header        = FromBytes <ATOM_ROM_HEADER>(Buffer.Skip(atom_rom_header_offset).ToArray());
                deviceID = atom_rom_header.usDeviceID.ToString("X");
                fixChecksum(false, txtChecksum);

                var firmwareSignature = new string(atom_rom_header.uaFirmWareSignature);
                if (!firmwareSignature.Equals("ATOM"))
                {
                    MessageBox.Show("WARNING! BIOS Signature is not valid. Only continue if you are 100% sure what you are doing!");
                }

                var oMsgSupported = DialogResult.Yes;
                if (!supportedDeviceID.Contains(deviceID))
                {
                    oMsgSupported = MessageBox.Show(String.Format(PolarisBiosEditorResource.UNSUPPORTED_DEVICE_ID, deviceID),
                                                    PolarisBiosEditorResource.WARNING, MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                }

                if (oMsgSupported == DialogResult.Yes)
                {
                    var sb = new StringBuilder();

                    var ptr = atom_rom_header.usBIOS_BootupMessageOffset + 2;
                    while (ptr != -1)
                    {
                        var c = (char)Buffer[ptr];
                        if (c == '\0')
                        {
                            ptr = -1;
                        }
                        else if (c == '\n' || c == '\r')
                        {
                            ptr++;
                        }
                        else
                        {
                            sb.Append(c);
                            ptr++;
                        }
                    }

                    BIOS_BootupMessage = sb.ToString();

                    atom_data_table       = FromBytes <ATOM_DATA_TABLES>(Buffer.Skip(atom_rom_header.usMasterDataTableOffset).ToArray());
                    atom_powerplay_offset = atom_data_table.PowerPlayInfo;
                    atom_powerplay_table  = FromBytes <ATOM_POWERPLAY_TABLE>(Buffer.Skip(atom_powerplay_offset).ToArray());

                    atom_powertune_offset = atom_data_table.PowerPlayInfo + atom_powerplay_table.usPowerTuneTableOffset;
                    atom_powertune_table  = FromBytes <ATOM_POWERTUNE_TABLE>(Buffer.Skip(atom_powertune_offset).ToArray());

                    atom_fan_offset = atom_data_table.PowerPlayInfo + atom_powerplay_table.usFanTableOffset;
                    atom_fan_table  = FromBytes <ATOM_FAN_TABLE>(Buffer.Skip(atom_fan_offset).ToArray());

                    atom_mclk_table_offset = atom_data_table.PowerPlayInfo + atom_powerplay_table.usMclkDependencyTableOffset;
                    atom_mclk_table        = FromBytes <ATOM_MCLK_TABLE>(Buffer.Skip(atom_mclk_table_offset).ToArray());
                    atom_mclk_entries      = new ATOM_MCLK_ENTRY[atom_mclk_table.ucNumEntries];

                    for (var i = 0; i < atom_mclk_entries.Length; i++)
                    {
                        atom_mclk_entries[i] = FromBytes <ATOM_MCLK_ENTRY>(Buffer.Skip(atom_mclk_table_offset + Marshal.SizeOf(typeof(ATOM_MCLK_TABLE)) + Marshal.SizeOf(typeof(ATOM_MCLK_ENTRY)) * i).ToArray());
                    }

                    atom_sclk_table_offset = atom_data_table.PowerPlayInfo + atom_powerplay_table.usSclkDependencyTableOffset;
                    atom_sclk_table        = FromBytes <ATOM_SCLK_TABLE>(Buffer.Skip(atom_sclk_table_offset).ToArray());
                    atom_sclk_entries      = new ATOM_SCLK_ENTRY[atom_sclk_table.ucNumEntries];

                    for (var i = 0; i < atom_sclk_entries.Length; i++)
                    {
                        atom_sclk_entries[i] = FromBytes <ATOM_SCLK_ENTRY>(Buffer.Skip(atom_sclk_table_offset + Marshal.SizeOf(typeof(ATOM_SCLK_TABLE)) + Marshal.SizeOf(typeof(ATOM_SCLK_ENTRY)) * i).ToArray());
                    }

                    atom_vddc_table_offset = atom_data_table.PowerPlayInfo + atom_powerplay_table.usVddcLookupTableOffset;
                    atom_vddc_table        = FromBytes <ATOM_VOLTAGE_TABLE>(Buffer.Skip(atom_vddc_table_offset).ToArray());
                    atom_vddc_entries      = new ATOM_VOLTAGE_ENTRY[atom_vddc_table.ucNumEntries];

                    for (var i = 0; i < atom_vddc_table.ucNumEntries; i++)
                    {
                        atom_vddc_entries[i] = FromBytes <ATOM_VOLTAGE_ENTRY>(Buffer.Skip(atom_vddc_table_offset + Marshal.SizeOf(typeof(ATOM_VOLTAGE_TABLE)) + Marshal.SizeOf(typeof(ATOM_VOLTAGE_ENTRY)) * i).ToArray());
                    }

                    atom_vram_info_offset = atom_data_table.VRAM_Info;
                    atom_vram_info        = FromBytes <ATOM_VRAM_INFO>(Buffer.Skip(atom_vram_info_offset).ToArray());
                    atom_vram_entries     = new ATOM_VRAM_ENTRY[atom_vram_info.ucNumOfVRAMModule];
                    var atom_vram_entry_offset = atom_vram_info_offset + Marshal.SizeOf(typeof(ATOM_VRAM_INFO));
                    for (var i = 0; i < atom_vram_info.ucNumOfVRAMModule; i++)
                    {
                        atom_vram_entries[i]    = FromBytes <ATOM_VRAM_ENTRY>(Buffer.Skip(atom_vram_entry_offset).ToArray());
                        atom_vram_entry_offset += atom_vram_entries[i].usModuleSize;
                    }

                    atom_vram_timing_offset  = atom_vram_info_offset + atom_vram_info.usMemClkPatchTblOffset + 0x2E;
                    Atom_vram_timing_entries = new ATOM_VRAM_TIMING_ENTRY[MAX_VRAM_ENTRIES];
                    for (var i = 0; i < MAX_VRAM_ENTRIES; i++)
                    {
                        Atom_vram_timing_entries[i] = FromBytes <ATOM_VRAM_TIMING_ENTRY>(Buffer.Skip(atom_vram_timing_offset + Marshal.SizeOf(typeof(ATOM_VRAM_TIMING_ENTRY)) * i).ToArray());

                        // atom_vram_timing_entries have an undetermined length
                        // attempt to determine the last entry in the array
                        if (Atom_vram_timing_entries[i].ulClkRange == 0)
                        {
                            ResizeVRAMTiming(i);
                            break;
                        }
                    }
                }
            }
        }
        private void OpenFileDialog_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter      = "BIOS (.rom)|*.rom|All Files (*.*)|*.*";
            openFileDialog.FilterIndex = 1;
            openFileDialog.Multiselect = false;

            if (openFileDialog.ShowDialog() == true)
            {
                save.IsEnabled = false;

                tableROM.Items.Clear();
                tablePOWERPLAY.Items.Clear();
                tablePOWERTUNE.Items.Clear();
                tableFAN.Items.Clear();
                tableGPU.Items.Clear();
                tableMEMORY.Items.Clear();
                tableVRAM.Items.Clear();
                tableVRAM_TIMING.Items.Clear();

                System.IO.Stream fileStream = openFileDialog.OpenFile();
                if (fileStream.Length < 524288)
                {
                    MessageBox.Show("This BIOS is less than the standard 512KB size.\nFlashing this BIOS may corrupt your graphics card.", "WARNING", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
                using (BinaryReader br = new BinaryReader(fileStream)) {
                    buffer = br.ReadBytes((int)fileStream.Length);

                    atom_rom_header_offset = getValueAtPosition(16, atom_rom_header_ptr);
                    atom_rom_header        = fromBytes <ATOM_ROM_HEADER>(buffer.Skip(atom_rom_header_offset).ToArray());
                    deviceID = atom_rom_header.usDeviceID.ToString("X");
                    fixChecksum(false);

                    MessageBoxResult msgSuported = MessageBoxResult.Yes;
                    if (!supportedDeviceID.Contains(deviceID))
                    {
                        msgSuported = MessageBox.Show("Unsupported DeviceID 0x" + deviceID + " - Continue?", "WARNING", MessageBoxButton.YesNo, MessageBoxImage.Warning);
                    }
                    if (msgSuported == MessageBoxResult.Yes)
                    {
                        atom_data_table       = fromBytes <ATOM_DATA_TABLES>(buffer.Skip(atom_rom_header.usMasterDataTableOffset).ToArray());
                        atom_powerplay_offset = atom_data_table.PowerPlayInfo;
                        atom_powerplay_table  = fromBytes <ATOM_POWERPLAY_TABLE>(buffer.Skip(atom_powerplay_offset).ToArray());

                        atom_powertune_offset = atom_data_table.PowerPlayInfo + atom_powerplay_table.usPowerTuneTableOffset;
                        atom_powertune_table  = fromBytes <ATOM_POWERTUNE_TABLE>(buffer.Skip(atom_powertune_offset).ToArray());

                        atom_fan_offset = atom_data_table.PowerPlayInfo + atom_powerplay_table.usFanTableOffset;
                        atom_fan_table  = fromBytes <ATOM_FAN_TABLE>(buffer.Skip(atom_fan_offset).ToArray());

                        atom_mclk_table_offset = atom_data_table.PowerPlayInfo + atom_powerplay_table.usMclkDependencyTableOffset;
                        atom_mclk_table        = fromBytes <ATOM_MCLK_TABLE>(buffer.Skip(atom_mclk_table_offset).ToArray());
                        atom_mclk_entries      = new ATOM_MCLK_ENTRY[atom_mclk_table.ucNumEntries];
                        for (var i = 0; i < atom_mclk_entries.Length; i++)
                        {
                            atom_mclk_entries[i] = fromBytes <ATOM_MCLK_ENTRY>(buffer.Skip(atom_mclk_table_offset + Marshal.SizeOf(typeof(ATOM_MCLK_TABLE)) + Marshal.SizeOf(typeof(ATOM_MCLK_ENTRY)) * i).ToArray());
                        }

                        atom_sclk_table_offset = atom_data_table.PowerPlayInfo + atom_powerplay_table.usSclkDependencyTableOffset;
                        atom_sclk_table        = fromBytes <ATOM_SCLK_TABLE>(buffer.Skip(atom_sclk_table_offset).ToArray());
                        atom_sclk_entries      = new ATOM_SCLK_ENTRY[atom_sclk_table.ucNumEntries];
                        for (var i = 0; i < atom_sclk_entries.Length; i++)
                        {
                            atom_sclk_entries[i] = fromBytes <ATOM_SCLK_ENTRY>(buffer.Skip(atom_sclk_table_offset + Marshal.SizeOf(typeof(ATOM_SCLK_TABLE)) + Marshal.SizeOf(typeof(ATOM_SCLK_ENTRY)) * i).ToArray());
                        }

                        atom_vddc_table_offset = atom_data_table.PowerPlayInfo + atom_powerplay_table.usVddcLookupTableOffset;
                        atom_vddc_table        = fromBytes <ATOM_VOLTAGE_TABLE>(buffer.Skip(atom_vddc_table_offset).ToArray());
                        atom_vddc_entries      = new ATOM_VOLTAGE_ENTRY[atom_vddc_table.ucNumEntries];
                        for (var i = 0; i < atom_vddc_table.ucNumEntries; i++)
                        {
                            atom_vddc_entries[i] = fromBytes <ATOM_VOLTAGE_ENTRY>(buffer.Skip(atom_vddc_table_offset + Marshal.SizeOf(typeof(ATOM_VOLTAGE_TABLE)) + Marshal.SizeOf(typeof(ATOM_VOLTAGE_ENTRY)) * i).ToArray());
                        }

                        atom_vram_info_offset = atom_data_table.VRAM_Info;
                        atom_vram_info        = fromBytes <ATOM_VRAM_INFO>(buffer.Skip(atom_vram_info_offset).ToArray());
                        atom_vram_entries     = new ATOM_VRAM_ENTRY[atom_vram_info.ucNumOfVRAMModule];
                        var atom_vram_entry_offset = atom_vram_info_offset + Marshal.SizeOf(typeof(ATOM_VRAM_INFO));
                        for (var i = 0; i < atom_vram_info.ucNumOfVRAMModule; i++)
                        {
                            atom_vram_entries[i]    = fromBytes <ATOM_VRAM_ENTRY>(buffer.Skip(atom_vram_entry_offset).ToArray());
                            atom_vram_entry_offset += atom_vram_entries[i].usModuleSize;
                        }
                        atom_vram_timing_entries = new ATOM_VRAM_TIMING_ENTRY[16];
                        for (var i = 0; i < 16; i++)
                        {
                            atom_vram_timing_entries[i] = fromBytes <ATOM_VRAM_TIMING_ENTRY>(buffer.Skip(atom_vram_entry_offset + 0x3D + Marshal.SizeOf(typeof(ATOM_VRAM_TIMING_ENTRY)) * i).ToArray());

                            // atom_vram_timing_entries have an undetermined length
                            // attempt to determine the last entry in the array
                            if (atom_vram_timing_entries[i].ulClkRange == 0)
                            {
                                Array.Resize(ref atom_vram_timing_entries, i);
                                break;
                            }
                        }

                        tableROM.Items.Clear();
                        tableROM.Items.Add(new {
                            NAME  = "VendorID",
                            VALUE = "0x" + atom_rom_header.usVendorID.ToString("X")
                        });
                        tableROM.Items.Add(new {
                            NAME  = "DeviceID",
                            VALUE = "0x" + atom_rom_header.usDeviceID.ToString("X")
                        });
                        tableROM.Items.Add(new {
                            NAME  = "Sub ID",
                            VALUE = "0x" + atom_rom_header.usSubsystemID.ToString("X")
                        });
                        tableROM.Items.Add(new {
                            NAME  = "Sub VendorID",
                            VALUE = "0x" + atom_rom_header.usSubsystemVendorID.ToString("X")
                        });
                        tableROM.Items.Add(new {
                            NAME  = "Firmware Signature",
                            VALUE = "0x" + atom_rom_header.uaFirmWareSignature.ToString("X")
                        });

                        tablePOWERPLAY.Items.Clear();
                        tablePOWERPLAY.Items.Add(new {
                            NAME  = "Max GPU Freq. (MHz)",
                            VALUE = atom_powerplay_table.ulMaxODEngineClock / 100
                        });
                        tablePOWERPLAY.Items.Add(new {
                            NAME  = "Max Memory Freq. (MHz)",
                            VALUE = atom_powerplay_table.ulMaxODMemoryClock / 100
                        });
                        tablePOWERPLAY.Items.Add(new {
                            NAME  = "Power Control Limit (%)",
                            VALUE = atom_powerplay_table.usPowerControlLimit
                        });

                        tablePOWERTUNE.Items.Clear();
                        tablePOWERTUNE.Items.Add(new {
                            NAME  = "TDP (W)",
                            VALUE = atom_powertune_table.usTDP
                        });
                        tablePOWERTUNE.Items.Add(new {
                            NAME  = "TDC (A)",
                            VALUE = atom_powertune_table.usTDC
                        });
                        tablePOWERTUNE.Items.Add(new {
                            NAME  = "Max Power Limit (W)",
                            VALUE = atom_powertune_table.usMaximumPowerDeliveryLimit
                        });
                        tablePOWERTUNE.Items.Add(new {
                            NAME  = "Max Temp. (C)",
                            VALUE = atom_powertune_table.usTjMax
                        });
                        tablePOWERTUNE.Items.Add(new {
                            NAME  = "Shutdown Temp. (C)",
                            VALUE = atom_powertune_table.usSoftwareShutdownTemp
                        });
                        tablePOWERTUNE.Items.Add(new {
                            NAME  = "Hotspot Temp. (C)",
                            VALUE = atom_powertune_table.usTemperatureLimitHotspot
                        });

                        tableFAN.Items.Clear();
                        tableFAN.Items.Add(new {
                            NAME  = "Temp. Hysteresis",
                            VALUE = atom_fan_table.ucTHyst
                        });
                        tableFAN.Items.Add(new {
                            NAME  = "Min Temp. (C)",
                            VALUE = atom_fan_table.usTMin / 100
                        });
                        tableFAN.Items.Add(new {
                            NAME  = "Med Temp. (C)",
                            VALUE = atom_fan_table.usTMed / 100
                        });
                        tableFAN.Items.Add(new {
                            NAME  = "High Temp. (C)",
                            VALUE = atom_fan_table.usTHigh / 100
                        });
                        tableFAN.Items.Add(new {
                            NAME  = "Max Temp. (C)",
                            VALUE = atom_fan_table.usTMax / 100
                        });
                        tableFAN.Items.Add(new {
                            NAME  = "Min PWM (%)",
                            VALUE = atom_fan_table.usPWMMin / 100
                        });
                        tableFAN.Items.Add(new {
                            NAME  = "Med PWM (%)",
                            VALUE = atom_fan_table.usPWMMed / 100
                        });
                        tableFAN.Items.Add(new {
                            NAME  = "High PWM (%)",
                            VALUE = atom_fan_table.usPWMHigh / 100
                        });
                        tableFAN.Items.Add(new {
                            NAME  = "Max PWM (%)",
                            VALUE = atom_fan_table.usFanPWMMax
                        });
                        tableFAN.Items.Add(new {
                            NAME  = "Max RPM",
                            VALUE = atom_fan_table.usFanRPMMax
                        });
                        tableFAN.Items.Add(new {
                            NAME  = "Sensitivity",
                            VALUE = atom_fan_table.usFanOutputSensitivity
                        });
                        tableFAN.Items.Add(new {
                            NAME  = "Acoustic Limit (MHz)",
                            VALUE = atom_fan_table.ulMinFanSCLKAcousticLimit / 100
                        });

                        tableGPU.Items.Clear();
                        for (var i = 0; i < atom_sclk_table.ucNumEntries; i++)
                        {
                            tableGPU.Items.Add(new {
                                MHZ = atom_sclk_entries[i].ulSclk / 100,
                                MV  = atom_vddc_entries[atom_sclk_entries[i].ucVddInd].usVdd
                            });
                        }

                        tableMEMORY.Items.Clear();
                        for (var i = 0; i < atom_mclk_table.ucNumEntries; i++)
                        {
                            tableMEMORY.Items.Add(new {
                                MHZ = atom_mclk_entries[i].ulMclk / 100,
                                MV  = atom_mclk_entries[i].usMvdd
                            });
                        }

                        listVRAM.Items.Clear();
                        for (var i = 0; i < atom_vram_info.ucNumOfVRAMModule; i++)
                        {
                            String vram = Encoding.UTF8.GetString(atom_vram_entries[i].strMemPNString);

                            if (vram.IndexOf('\0') > 0)
                            {
                                vram = vram.Remove(vram.IndexOf('\0'));
                            }

                            listVRAM.Items.Add(vram);
                        }
                        listVRAM.SelectedIndex = 0;
                        atom_vram_index        = listVRAM.SelectedIndex;

                        tableVRAM_TIMING.Items.Clear();
                        for (var i = 0; i < atom_vram_timing_entries.Length; i++)
                        {
                            tableVRAM_TIMING.Items.Add(new {
                                MHZ   = atom_vram_timing_entries[i].ulClkRange / 100,
                                VALUE = ByteArrayToString(atom_vram_timing_entries[i].ucLatency)
                            });
                        }

                        save.IsEnabled         = true;
                        boxROM.IsEnabled       = true;
                        boxPOWERPLAY.IsEnabled = true;
                        boxPOWERTUNE.IsEnabled = true;
                        boxFAN.IsEnabled       = true;
                        boxGPU.IsEnabled       = true;
                        boxMEM.IsEnabled       = true;
                        boxVRAM.IsEnabled      = true;
                    }
                    fileStream.Close();
                }
            }
        }