-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAOD.cs
414 lines (355 loc) · 14.3 KB
/
AOD.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Management;
using static ZenStates.Core.ACPI;
namespace ZenStates.Core
{
public class AOD
{
internal readonly IOModule io;
internal readonly Cpu cpuInstance;
internal readonly ACPI acpi;
internal readonly Cpu.CodeName codeName;
internal readonly uint patchLevel;
internal readonly bool hasRMP;
public AodTable Table;
public class AodEnumBase
{
protected int Value;
public AodEnumBase(int value)
{
Value = value;
}
public override string ToString()
{
return GetByKey(ValueDictionary, Value);
}
protected virtual Dictionary<int, string> ValueDictionary { get; } = new Dictionary<int, string>();
protected static string GetByKey(Dictionary<int, string> dictionary, int key)
{
return dictionary.TryGetValue(key, out string output) ? output : @"N/A";
}
}
public class ProcOdt : AodEnumBase
{
public ProcOdt(int value) : base(value) { }
protected override Dictionary<int, string> ValueDictionary { get; } = AodDictionaries.ProcOdtDict;
}
public class ProcDataDrvStren : AodEnumBase
{
public ProcDataDrvStren(int value) : base(value) { }
protected override Dictionary<int, string> ValueDictionary { get; } = AodDictionaries.ProcDataDrvStrenDict;
}
public class DramDataDrvStren : AodEnumBase
{
public DramDataDrvStren(int value) : base(value) { }
protected override Dictionary<int, string> ValueDictionary { get; } = AodDictionaries.DramDataDrvStrenDict;
}
public class CadBusDrvStren : AodEnumBase
{
public CadBusDrvStren(int value) : base(value) { }
protected override Dictionary<int, string> ValueDictionary { get; } = AodDictionaries.CadBusDrvStrenDict;
}
public class ProcOdtImpedance : AodEnumBase
{
public ProcOdtImpedance(int value) : base(value) { }
protected override Dictionary<int, string> ValueDictionary { get; } = AodDictionaries.ProcOdtImpedanceDict;
}
public class Rtt : AodEnumBase
{
public Rtt(int value) : base(value) { }
protected override Dictionary<int, string> ValueDictionary { get; } = AodDictionaries.RttDict;
public override string ToString()
{
string value = base.ToString();
if (this.Value > 0)
return $"{value} ({240 / Value})";
return $"{value}";
}
}
public class Voltage
{
protected int Value;
public Voltage(int value)
{
Value = value;
}
public override string ToString()
{
return string.Format(CultureInfo.GetCultureInfo("en-US"), "{0:F4}V", Value / 1000.0);
}
}
[Serializable]
public class AodTable
{
public readonly uint Signature;
public ulong OemTableId;
// public readonly byte[] RegionSignature;
public uint BaseAddress;
public int Length;
public ACPITable? AcpiTable;
public AodData Data;
public byte[] RawAodTable;
public AodTable()
{
this.Signature = Signature(TableSignature.SSDT);
this.OemTableId = SignatureUL(TableSignature.AOD_);
//this.RegionSignature = ByteSignature(TableSignature.AODE);
}
}
public AOD(IOModule io, Cpu cpuInstance)
{
this.io = io;
this.cpuInstance = cpuInstance;
this.codeName = this.cpuInstance.info.codeName;
this.acpi = new ACPI(io);
this.Table = new AodTable();
this.patchLevel = this.cpuInstance.info.patchLevel;
this.hasRMP = GetWmiFunctions().ContainsKey("Set RMP Profile");
this.Init();
}
private ACPITable? GetAcpiTable()
{
// Try to get the table from RSDT first
ACPITable? acpiTable = GetAcpiTableFromRsdt();
if (acpiTable == null)
return AOD.GetAcpiTableFromRegistry();
return acpiTable;
}
private ACPITable? GetAcpiTableFromRsdt()
{
try
{
RSDT rsdt = acpi.GetRsdt();
foreach (uint addr in rsdt.Data)
{
if (addr != 0)
{
try
{
SDTHeader hdr = acpi.GetHeader<SDTHeader>(addr);
if (
hdr.Signature == this.Table.Signature
&& (hdr.OEMTableID == this.Table.OemTableId ||
hdr.OEMTableID == SignatureUL(TableSignature.AAOD) ||
hdr.OEMTableID == SignatureUL(TableSignature.LENOVO_AOD))
)
{
return ParseSdtTable(io.ReadMemory(new IntPtr(addr), (int)hdr.Length));
}
}
catch { }
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error getting SSDT ACPI table from RSDT: {ex.Message}");
}
return null;
}
private static ACPITable? GetAcpiTableFromRegistry()
{
string acpiRegistryPath = @"HARDWARE\ACPI";
RegistryKey acpiKey = null;
try
{
acpiKey = Registry.LocalMachine.OpenSubKey(acpiRegistryPath);
if (acpiKey != null)
{
string[] subkeyNames = acpiKey.GetSubKeyNames();
foreach (string subkeyName in subkeyNames)
{
Console.WriteLine($"Subkey: {subkeyName}");
if (subkeyName.StartsWith("SSD"))
{
byte[] acpiTableData = GetRawTableFromSubkeys(acpiKey, subkeyName);
if (acpiTableData != null)
{
if (GetAodRegionIndex(acpiTableData) == -1)
continue;
return ParseSdtTable(acpiTableData);
}
}
}
}
else
{
Console.WriteLine("ACPI registry key not found.");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error accessing ACPI registry: {ex.Message}");
}
finally
{
acpiKey?.Close();
}
return null;
}
private static int GetAodRegionIndex(byte[] rawTable)
{
if (rawTable == null)
return -1;
int regionIndex = Utils.FindSequence(rawTable, 0, ByteSignature(TableSignature.AODE));
if (regionIndex == -1)
regionIndex = Utils.FindSequence(rawTable, 0, ByteSignature(TableSignature.AODT));
return regionIndex;
}
private static byte[] GetRawTableFromSubkeys(RegistryKey parentKey, string subkeyName)
{
RegistryKey subkey = null;
try
{
subkey = parentKey.OpenSubKey(subkeyName);
if (subkey != null)
{
string[] subkeyNames = subkey.GetSubKeyNames();
if (subkeyNames.Length == 0)
{
// Found a key without further subkeys, retrieve the first REG_BINARY value
foreach (string valueName in subkey.GetValueNames())
{
object value = subkey.GetValue(valueName);
if (value is byte[] rawTable)
{
return rawTable;
}
}
Console.WriteLine($"No REG_BINARY value found in {subkeyName}.");
}
else
{
// Continue drilling down if there are more subkeys
foreach (string nestedSubkeyName in subkeyNames)
{
Console.WriteLine($"Nested Subkey: {nestedSubkeyName}");
byte[] rawTable = GetRawTableFromSubkeys(subkey, nestedSubkeyName);
if (rawTable != null)
{
return rawTable;
}
}
}
}
}
finally
{
subkey?.Close();
}
return null;
}
private void Init()
{
this.Table.AcpiTable = GetAcpiTable();
if (this.Table?.AcpiTable?.Data != null)
{
int regionIndex = GetAodRegionIndex(this.Table.AcpiTable.Value.Data);
if (regionIndex == -1)
return;
byte[] region = new byte[16];
Buffer.BlockCopy(this.Table.AcpiTable.Value.Data, regionIndex, region, 0, 16);
// OperationRegion(AODE, SystemMemory, Offset, Length)
OperationRegion opRegion = Utils.ByteArrayToStructure<OperationRegion>(region);
this.Table.BaseAddress = opRegion.Offset;
this.Table.Length = (opRegion.Length[1] << 8) | opRegion.Length[0];
}
this.Refresh();
}
private Dictionary<string, int> GetAodDataDictionary(Cpu.CodeName codeName, uint patchLevel)
{
if (Table.AcpiTable.Value.Header.OEMTableID == TableSignature.LENOVO_AOD)
return AodDictionaries.AodDataDictionaryV3;
switch (codeName)
{
case Cpu.CodeName.StormPeak:
case Cpu.CodeName.Genoa:
case Cpu.CodeName.DragonRange:
return AodDictionaries.AodDataDictionaryV2;
case Cpu.CodeName.Phoenix:
case Cpu.CodeName.Phoenix2:
case Cpu.CodeName.HawkPoint:
return AodDictionaries.AodDataDictionaryV4;
case Cpu.CodeName.GraniteRidge:
var memModule = cpuInstance.GetMemoryConfig()?.Modules[0];
var isMDie = memModule?.Rank == DRAM.MemRank.SR && memModule.AddressConfig.NumRow > 16;
var isDR = memModule?.Rank == DRAM.MemRank.DR;
if (patchLevel > 0xB404022)
{
if (isMDie && hasRMP)
return AodDictionaries.AodDataDictionary_1Ah_B404023;
if (isMDie || isDR)
return AodDictionaries.AodDataDictionary_1Ah_B404023_M;
return AodDictionaries.AodDataDictionary_1Ah_B404023;
}
if (isMDie && hasRMP)
return AodDictionaries.AodDataDictionary_1Ah_M;
return AodDictionaries.AodDataDictionary_1Ah;
default:
return AodDictionaries.AodDataDictionaryV1;
}
}
private static Dictionary<string, uint> GetWmiFunctions()
{
Dictionary<string, uint> dict = new Dictionary<string, uint>();
try
{
string wmiAMDACPI = "AMD_ACPI";
string wmiScope = "root\\wmi";
ManagementBaseObject pack;
string instanceName = WMI.GetInstanceName(wmiScope, wmiAMDACPI);
if (String.IsNullOrEmpty(instanceName))
return dict;
ManagementObject classInstance = new ManagementObject(wmiScope,
$"{wmiAMDACPI}.InstanceName='{instanceName}'",
null);
// Get function names with their IDs
string[] functionObjects = { "GetObjectID", "GetObjectID2" };
foreach (var functionObject in functionObjects)
{
try
{
pack = WMI.InvokeMethodAndGetValue(classInstance, functionObject, "pack", null, 0);
if (pack != null)
{
var ID = (uint[])pack.GetPropertyValue("ID");
var IDString = (string[])pack.GetPropertyValue("IDString");
var Length = (byte)pack.GetPropertyValue("Length");
for (var i = 0; i < Length; ++i)
{
if (IDString[i] == "")
break;
dict.Add(IDString[i], ID[i]);
}
}
}
catch
{
// ignored
}
}
}
catch
{
// ignored
}
return dict;
}
public bool Refresh()
{
try
{
this.Table.RawAodTable = this.io.ReadMemory(new IntPtr(this.Table.BaseAddress), this.Table.Length);
// this.Table.Data = Utils.ByteArrayToStructure<AodData>(this.Table.rawAodTable);
// int test = Utils.FindSequence(rawTable, 0, BitConverter.GetBytes(0x3ae));
this.Table.Data = AodData.CreateFromByteArray(this.Table.RawAodTable, GetAodDataDictionary(this.codeName, this.patchLevel));
return true;
}
catch { }
return false;
}
}
}