Skip to content

Commit 6544e1d

Browse files
author
Vasily Galkin
committed
try not crashing even on unsupported vbioses that can be partially parsed
fix building & printing stacktrace on linux bump version to 2021-08
1 parent 4283343 commit 6544e1d

File tree

3 files changed

+79
-36
lines changed

3 files changed

+79
-36
lines changed

PolarisBiosEditor.cs

Lines changed: 77 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public partial class PolarisBiosEditor : Form
110110

111111
/* DATA */
112112

113-
string version = "1.7xml-2021.07";
113+
string version = "1.7xml-2021.08";
114114
string programTitle = "PolarisBiosEditor";
115115

116116

@@ -748,7 +748,7 @@ public string KindInNamespace
748748
{
749749
get
750750
{
751-
var type_name = Namespace.ToString().Split(new[] { '_' }).Last();
751+
var type_name = Namespace.Split(new[] { '_' }).Last();
752752
var type = Type.GetType("PolarisBiosEditor.KIND_" + type_name);
753753
if (type != null)
754754
{
@@ -758,9 +758,9 @@ public string KindInNamespace
758758
}
759759
set { throw new NotImplementedException(); }
760760
}
761-
public GRAPH_OBJECT_TYPE Namespace
761+
public string Namespace
762762
{
763-
get { return (GRAPH_OBJECT_TYPE)(NamespaceAndIndex >> 4); }
763+
get { return EnumOrValue<GRAPH_OBJECT_TYPE>(NamespaceAndIndex >> 4); }
764764
set { throw new NotImplementedException(); }
765765
}
766766
public int Index
@@ -1139,7 +1139,7 @@ public ConsecutiveReader(Byte[] entire_buffer, int offset, PolarisBiosEditor a_e
11391139
public T Read()
11401140
{
11411141
T obj = default(T);
1142-
int size = Marshal.SizeOf(obj);
1142+
int size = Marshal.SizeOf<T>();
11431143
IntPtr ptr = Marshal.AllocHGlobal(size);
11441144

11451145
Marshal.Copy(buffer.Array, buffer.Offset, ptr, size);
@@ -1150,8 +1150,13 @@ public T Read()
11501150
}
11511151
public T ReadPrint(string name = "")
11521152
{
1153+
int size = Marshal.SizeOf<T>();
1154+
if (buffer.Offset + size > buffer.Array.Length)
1155+
{
1156+
editor.Print("IGNORED_OUT_OF_RANGE_INSTANCE_OF_"+typeof(T).Name, "addr", string.Format("0x{0:X}-0x{1:X} len={2}=0x{2:X}{3}", buffer.Offset, buffer.Offset + size, size, name));
1157+
return default(T);
1158+
}
11531159
T result = Read();
1154-
int size = Marshal.SizeOf(typeof(T));
11551160
if (!string.IsNullOrWhiteSpace(name))
11561161
{
11571162
name = " Name=" + name;
@@ -1167,7 +1172,14 @@ public void Jump1Structure()
11671172
public void Jump(int relative_offset)
11681173
{
11691174
int offset = buffer.Offset + relative_offset;
1170-
buffer = new ArraySegment<byte>(buffer.Array, offset, buffer.Array.Length - offset);
1175+
if (offset < 0 || offset >= buffer.Array.Length)
1176+
{
1177+
buffer = new ArraySegment<byte>(buffer.Array, 0, 0);
1178+
}
1179+
else
1180+
{
1181+
buffer = new ArraySegment<byte>(buffer.Array, offset, buffer.Array.Length - offset);
1182+
}
11711183
}
11721184
public void JumpPrintExtra(int relative_offset)
11731185
{
@@ -1244,11 +1256,20 @@ public void setBytesAtPosition(byte[] dest, int ptr, byte[] src)
12441256
}
12451257
}
12461258

1259+
public static string EnumOrValue<TEnum>(int value)
1260+
{
1261+
return ((TEnum)Enum.ToObject(typeof(TEnum),value)).ToString();
1262+
}
12471263
public string HexRange(int start, int len)
12481264
{
12491265
string values = "";
12501266
for (int i =0; i<len; ++i)
12511267
{
1268+
if (start + i >= buffer.Length)
1269+
{
1270+
values += "!END_OF_INPUT_DATA!";
1271+
break;
1272+
}
12521273
if (i <14 || i > len-15)
12531274
{
12541275
values += string.Format("{0:X2}", buffer[start + i]);
@@ -1329,25 +1350,38 @@ public void PrintCmds(ATOM_CMD_TABLES_LIST cmds, int final_offset)
13291350

13301351
public void PrintCheckFF(int start, int after_end)
13311352
{
1353+
if (start < 0)
1354+
{
1355+
PrintXml("INVALID_NEGATIVE_START_PBinaryAreaFastCheck");
1356+
return;
1357+
}
13321358
int FF_count = 0;
1333-
int NonFF_count = 0;
1334-
for (int i = start; i < after_end; ++i)
1359+
int Zero_count = 0;
1360+
int Other_count = 0;
1361+
for (int i = start; i < Math.Min(after_end, buffer.Length); ++i)
13351362
{
13361363
if (buffer[i] == 0xFF) ++FF_count;
1337-
else ++NonFF_count;
1364+
else if (buffer[i] == 0) ++Zero_count;
1365+
else ++Other_count;
13381366
}
1339-
PrintXml("FromLastToSectionEnd", "", new Attrs{
1367+
PrintXml("BinaryAreaFastCheck", "", new Attrs{
13401368
{"info", HexRange(start, after_end - start)},
13411369
{"FF_count", FF_count},
1342-
{"NonFF_count", NonFF_count},
1370+
{"Zero_count", Zero_count},
1371+
{"Other_count", Other_count},
13431372
});
13441373
}
13451374

13461375
public void PrintCheckText(int start, int after_end)
13471376
{
1377+
var as_text_segment = new ArraySegment<byte>(buffer, 0, 0);
1378+
if (start < buffer.Length)
1379+
{
1380+
as_text_segment = new ArraySegment<byte>(buffer, start, Math.Min(after_end, buffer.Length) - start);
1381+
}
13481382
PrintXml("MostlyText", "", new Attrs{
13491383
{"info", HexRange(start, after_end - start)},
1350-
{"as_text", SafeDecodeAscii(new ArraySegment<byte>(buffer, start, after_end-start))},
1384+
{"as_text", SafeDecodeAscii(as_text_segment)},
13511385
});
13521386
}
13531387

@@ -1477,7 +1511,7 @@ private int PrintAndReturnLen(ATOM_OBJECT o)
14771511
if (o.usRecordOffset != 0)
14781512
{
14791513
var reader = Reader<ATOM_COMMON_RECORD_HEADER>(atom_data_table.Object_Header + o.usRecordOffset);
1480-
while (true)
1514+
while (reader.buffer.Count > 0)
14811515
{
14821516
var first = reader.buffer.First();
14831517
if (first == 0 || first == 255)
@@ -1707,13 +1741,13 @@ private void ParseVbiosAndClose(Stream fileStream, string filename)
17071741

17081742
if (SafeDecodeAscii(atom_rom_header.uaFirmWareSignature) != "ATOM")
17091743
{
1710-
MessageBox.Show("WARNING! BIOS Signature is not valid. Only continue if you are 100% sure what you are doing!");
1744+
MessageBox.Show("WARNING! BIOS doesn't look like valid AMD GPU Bios. Results may be garbage.", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning);
17111745
}
17121746

17131747
string deviceId = pcir_header.usDeviceID.ToString("X");
17141748
if (!supportedID.Contains(deviceId))
17151749
{
1716-
MessageBox.Show("Unsupported DeviceID 0x" + deviceId + " - Continue?", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning);
1750+
MessageBox.Show("Unsupported DeviceID 0x" + deviceId + ".\n You can view fileds and XML output, but trying to save will crash or produce corrupted file", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning);
17171751
}
17181752

17191753
atom_code = Reader<ATOM_CMD_TABLES>(atom_rom_header.usMasterCommandTableOffset).ReadPrint();
@@ -1740,14 +1774,14 @@ private void ParseVbiosAndClose(Stream fileStream, string filename)
17401774
atom_vddc_entries = new ATOM_VOLTAGE_ENTRY[atom_vddc_table.ucNumEntries];
17411775
for (var i = 0; i < atom_vddc_table.ucNumEntries; i++)
17421776
{
1743-
atom_vddc_entries[i] = Reader<ATOM_VOLTAGE_ENTRY>(atom_vddc_table_offset + Marshal.SizeOf(typeof(ATOM_VOLTAGE_TABLE)) + Marshal.SizeOf(typeof(ATOM_VOLTAGE_ENTRY)) * i).ReadPrint();
1777+
atom_vddc_entries[i] = Reader<ATOM_VOLTAGE_ENTRY>(atom_vddc_table_offset + Marshal.SizeOf<ATOM_VOLTAGE_TABLE>() + Marshal.SizeOf<ATOM_VOLTAGE_ENTRY>() * i).ReadPrint();
17441778
}
17451779

17461780
var atom_vddgfx_table_offset = atom_data_table.PowerPlayInfo + atom_powerplay_table.usVddgfxLookupTableOffset;
17471781
var atom_vddgfx_table = Reader<ATOM_VOLTAGE_TABLE>(atom_vddgfx_table_offset).ReadPrint("VddgfxLookupTable");
17481782
for (var i = 0; i < atom_vddgfx_table.ucNumEntries; i++)
17491783
{
1750-
Reader<ATOM_VOLTAGE_ENTRY>(atom_vddgfx_table_offset + Marshal.SizeOf(typeof(ATOM_VOLTAGE_TABLE)) + Marshal.SizeOf(typeof(ATOM_VOLTAGE_ENTRY)) * i).ReadPrint();
1784+
Reader<ATOM_VOLTAGE_ENTRY>(atom_vddgfx_table_offset + Marshal.SizeOf<ATOM_VOLTAGE_TABLE>() + Marshal.SizeOf<ATOM_VOLTAGE_ENTRY>() * i).ReadPrint();
17511785
}
17521786
}
17531787

@@ -1758,15 +1792,15 @@ private void ParseVbiosAndClose(Stream fileStream, string filename)
17581792
atom_sclk_entries = new ATOM_SCLK_ENTRY[atom_sclk_table.ucNumEntries];
17591793
for (var i = 0; i < atom_sclk_entries.Length; i++)
17601794
{
1761-
atom_sclk_entries[i] = Reader<ATOM_SCLK_ENTRY>(atom_sclk_table_offset + Marshal.SizeOf(typeof(ATOM_SCLK_TABLE)) + Marshal.SizeOf(typeof(ATOM_SCLK_ENTRY)) * i).ReadPrint();
1795+
atom_sclk_entries[i] = Reader<ATOM_SCLK_ENTRY>(atom_sclk_table_offset + Marshal.SizeOf<ATOM_SCLK_TABLE>() + Marshal.SizeOf<ATOM_SCLK_ENTRY>() * i).ReadPrint();
17621796
}
17631797

17641798
atom_mclk_table_offset = atom_data_table.PowerPlayInfo + atom_powerplay_table.usMclkDependencyTableOffset;
17651799
atom_mclk_table = Reader<ATOM_MCLK_TABLE>(atom_mclk_table_offset).ReadPrint("MclkDependencyTable");
17661800
atom_mclk_entries = new ATOM_MCLK_ENTRY[atom_mclk_table.ucNumEntries];
17671801
for (var i = 0; i < atom_mclk_entries.Length; i++)
17681802
{
1769-
atom_mclk_entries[i] = Reader<ATOM_MCLK_ENTRY>(atom_mclk_table_offset + Marshal.SizeOf(typeof(ATOM_MCLK_TABLE)) + Marshal.SizeOf(typeof(ATOM_MCLK_ENTRY)) * i).ReadPrint();
1803+
atom_mclk_entries[i] = Reader<ATOM_MCLK_ENTRY>(atom_mclk_table_offset + Marshal.SizeOf<ATOM_MCLK_TABLE>() + Marshal.SizeOf<ATOM_MCLK_ENTRY>() * i).ReadPrint();
17701804
}
17711805
}
17721806

@@ -1776,7 +1810,7 @@ private void ParseVbiosAndClose(Stream fileStream, string filename)
17761810
var atom_mm_dependency_table = Reader<ATOM_MM_DEPENDENCY_TABLE>(atom_mm_dependency_table_offset).ReadPrint();
17771811
for (var i = 0; i < atom_mm_dependency_table.ucNumEntries; i++)
17781812
{
1779-
Reader<ATOM_MM_DEPENDENCY_RECORD>(atom_mm_dependency_table_offset + Marshal.SizeOf(typeof(ATOM_MM_DEPENDENCY_TABLE)) + Marshal.SizeOf(typeof(ATOM_MM_DEPENDENCY_RECORD)) * i).ReadPrint();
1813+
Reader<ATOM_MM_DEPENDENCY_RECORD>(atom_mm_dependency_table_offset + Marshal.SizeOf<ATOM_MM_DEPENDENCY_TABLE>() + Marshal.SizeOf<ATOM_MM_DEPENDENCY_RECORD>() * i).ReadPrint();
17801814
}
17811815
}
17821816

@@ -1785,7 +1819,7 @@ private void ParseVbiosAndClose(Stream fileStream, string filename)
17851819

17861820
atom_powertune_offset = atom_data_table.PowerPlayInfo + atom_powerplay_table.usPowerTuneTableOffset;
17871821
atom_powertune_table = Reader<ATOM_Polaris_PowerTune_Table>(atom_powertune_offset).ReadPrint();
1788-
Debug.Assert(atom_powertune_table.ucRevId == 4, "Unknown version of ATOM_POWERTUNE_TABLE");
1822+
//actually only atom_powertune_table.ucRevId == 4 is supported
17891823

17901824
using(AutoClosingXml("AtomVideoOutTables"))
17911825
{
@@ -1811,7 +1845,7 @@ private void ParseVbiosAndClose(Stream fileStream, string filename)
18111845
atom_vram_info_offset = atom_data_table.VRAM_Info;
18121846
atom_vram_info = Reader<ATOM_VRAM_INFO>(atom_vram_info_offset).ReadPrint();
18131847
atom_vram_entries = new ATOM_VRAM_ENTRY[atom_vram_info.ucNumOfVRAMModule];
1814-
var atom_vram_entry_reader = Reader<ATOM_VRAM_ENTRY>(atom_vram_info_offset + Marshal.SizeOf(typeof(ATOM_VRAM_INFO)));
1848+
var atom_vram_entry_reader = Reader<ATOM_VRAM_ENTRY>(atom_vram_info_offset + Marshal.SizeOf<ATOM_VRAM_INFO>());
18151849
for (var i = 0; i < atom_vram_info.ucNumOfVRAMModule; i++)
18161850
{
18171851
atom_vram_entries[i] = atom_vram_entry_reader.ReadPrint();
@@ -1822,7 +1856,7 @@ private void ParseVbiosAndClose(Stream fileStream, string filename)
18221856
atom_vram_timing_entries = new ATOM_VRAM_TIMING_ENTRY[MAX_VRAM_ENTRIES];
18231857
for (var i = 0; i < MAX_VRAM_ENTRIES; i++)
18241858
{
1825-
atom_vram_timing_entries[i] = Reader<ATOM_VRAM_TIMING_ENTRY>(atom_vram_timing_offset + Marshal.SizeOf(typeof(ATOM_VRAM_TIMING_ENTRY)) * i).ReadPrint();
1859+
atom_vram_timing_entries[i] = Reader<ATOM_VRAM_TIMING_ENTRY>(atom_vram_timing_offset + Marshal.SizeOf<ATOM_VRAM_TIMING_ENTRY>() * i).ReadPrint();
18261860

18271861
// atom_vram_timing_entries have an undetermined length
18281862
// attempt to determine the last entry in the array
@@ -1909,7 +1943,7 @@ private void UpdateGui(string file_title)
19091943
StringBuilder bios_bootup_builder = new StringBuilder();
19101944

19111945
Int32 ptr = atom_rom_header.usBIOS_BootupMessageOffset + 2;
1912-
while (ptr != -1)
1946+
while (ptr >= 0 && ptr < buffer.Length)
19131947
{
19141948
Char c = (Char)buffer[ptr];
19151949
if (c == '\0')
@@ -2093,9 +2127,15 @@ private void UpdateGui(string file_title)
20932127
tableGPU.Items.Clear();
20942128
for (var i = 0; i < atom_sclk_table.ucNumEntries; i++)
20952129
{
2130+
UInt16 usVdd = 0;
2131+
var usVddInd = atom_sclk_entries [i].ucVddInd;
2132+
if (atom_vddc_entries.Length > usVddInd)
2133+
{
2134+
usVdd = atom_vddc_entries[usVddInd].usVdd;
2135+
}
20962136
tableGPU.Items.Add(new ListViewItem(new string[] {
20972137
Convert.ToString (atom_sclk_entries [i].ulSclk / 100),
2098-
Convert.ToString (atom_vddc_entries [atom_sclk_entries [i].ucVddInd].usVdd)
2138+
Convert.ToString (usVdd)
20992139
}
21002140
));
21012141
}
@@ -2118,7 +2158,7 @@ private void UpdateGui(string file_title)
21182158
if (atom_vram_entries[i].strMemPNString[0] != 0)
21192159
{
21202160
var mem_id_full = atom_vram_entries[i].FullName;
2121-
var mem_id = mem_id_full.Substring(0, 10);
2161+
var mem_id = mem_id_full.Substring(0, Math.Min(10, mem_id_full.Length));
21222162
string mem_vendor;
21232163
if (rc.ContainsKey(mem_id))
21242164
{
@@ -2132,8 +2172,11 @@ private void UpdateGui(string file_title)
21322172
listVRAM.Items.Add(mem_id_full + "-" + mem_vendor);
21332173
}
21342174
}
2135-
listVRAM.SelectedIndex = 0;
2136-
atom_vram_index = listVRAM.SelectedIndex;
2175+
if (listVRAM.Items.Count > 0)
2176+
{
2177+
listVRAM.SelectedIndex = 0;
2178+
}
2179+
atom_vram_index = 0;
21372180

21382181
tableVRAM_TIMING.Items.Clear();
21392182
for (var i = 0; i < atom_vram_timing_entries.Length; i++)
@@ -2467,20 +2510,20 @@ private void SaveFileDialog_Click(object sender, EventArgs e)
24672510

24682511
for (var i = 0; i < atom_mclk_table.ucNumEntries; i++)
24692512
{
2470-
setBytesAtPosition(buffer, atom_mclk_table_offset + Marshal.SizeOf(typeof(ATOM_MCLK_TABLE)) + Marshal.SizeOf(typeof(ATOM_MCLK_ENTRY)) * i, getBytes(atom_mclk_entries[i]));
2513+
setBytesAtPosition(buffer, atom_mclk_table_offset + Marshal.SizeOf<ATOM_MCLK_TABLE>() + Marshal.SizeOf<ATOM_MCLK_ENTRY>() * i, getBytes(atom_mclk_entries[i]));
24712514
}
24722515

24732516
for (var i = 0; i < atom_sclk_table.ucNumEntries; i++)
24742517
{
2475-
setBytesAtPosition(buffer, atom_sclk_table_offset + Marshal.SizeOf(typeof(ATOM_SCLK_TABLE)) + Marshal.SizeOf(typeof(ATOM_SCLK_ENTRY)) * i, getBytes(atom_sclk_entries[i]));
2518+
setBytesAtPosition(buffer, atom_sclk_table_offset + Marshal.SizeOf<ATOM_SCLK_TABLE>() + Marshal.SizeOf<ATOM_SCLK_ENTRY>() * i, getBytes(atom_sclk_entries[i]));
24762519
}
24772520

24782521
for (var i = 0; i < atom_vddc_table.ucNumEntries; i++)
24792522
{
2480-
setBytesAtPosition(buffer, atom_vddc_table_offset + Marshal.SizeOf(typeof(ATOM_VOLTAGE_TABLE)) + Marshal.SizeOf(typeof(ATOM_VOLTAGE_ENTRY)) * i, getBytes(atom_vddc_entries[i]));
2523+
setBytesAtPosition(buffer, atom_vddc_table_offset + Marshal.SizeOf<ATOM_VOLTAGE_TABLE>() + Marshal.SizeOf<ATOM_VOLTAGE_ENTRY>() * i, getBytes(atom_vddc_entries[i]));
24812524
}
24822525

2483-
var atom_vram_entry_offset = atom_vram_info_offset + Marshal.SizeOf(typeof(ATOM_VRAM_INFO));
2526+
var atom_vram_entry_offset = atom_vram_info_offset + Marshal.SizeOf<ATOM_VRAM_INFO>();
24842527
for (var i = 0; i < atom_vram_info.ucNumOfVRAMModule; i++)
24852528
{
24862529
setBytesAtPosition(buffer, atom_vram_entry_offset, getBytes(atom_vram_entries[i]));
@@ -2490,7 +2533,7 @@ private void SaveFileDialog_Click(object sender, EventArgs e)
24902533
atom_vram_timing_offset = atom_vram_info_offset + atom_vram_info.usMemClkPatchTblOffset + 0x2E;
24912534
for (var i = 0; i < atom_vram_timing_entries.Length; i++)
24922535
{
2493-
setBytesAtPosition(buffer, atom_vram_timing_offset + Marshal.SizeOf(typeof(ATOM_VRAM_TIMING_ENTRY)) * i, getBytes(atom_vram_timing_entries[i]));
2536+
setBytesAtPosition(buffer, atom_vram_timing_offset + Marshal.SizeOf<ATOM_VRAM_TIMING_ENTRY>() * i, getBytes(atom_vram_timing_entries[i]));
24942537
}
24952538

24962539
BIOS_BootupMessage = txtBIOSBootupMessage.Text.Substring(0, BIOS_BootupMessage.Length);

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/bash
22
mkdir -p bin/Debug
33
#mono-mcs is required for building
4-
mcs --mcs-debug 1 -debug -define:DEBUG -lib:/usr/lib/mono/4.0 -r:System.Drawing.dll -r:System.Windows.Forms.dll -r:System.Xml.Linq.dll PolarisBiosEditor.cs PolarisBiosEditor.Designer.cs -out:bin/Debug/PolarisBiosEditor.exe
4+
mcs -sdk:4.6 --mcs-debug 1 -debug -define:DEBUG -r:System.Drawing.dll -r:System.Windows.Forms.dll -r:System.Xml.Linq.dll PolarisBiosEditor.cs PolarisBiosEditor.Designer.cs -out:bin/Debug/PolarisBiosEditor.exe

run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ SELF_DIR="`dirname "$SELF"`"
66
# libmono-system-windows-forms4.0-cil
77
# libmono-system-xml-linq4.0-cil
88

9-
mono "$SELF_DIR/bin/Debug/PolarisBiosEditor.exe" "$@"
9+
mono --debug "$SELF_DIR/bin/Debug/PolarisBiosEditor.exe" "$@"

0 commit comments

Comments
 (0)