Skip to content

Commit

Permalink
Try fix ayufan#176
Browse files Browse the repository at this point in the history
  • Loading branch information
ColdThunder11 committed Mar 18, 2024
1 parent 874cf6f commit 31817db
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
56 changes: 56 additions & 0 deletions CommonHelpers/InpOut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class InpOut : IDisposable
public UnmapPhysicalMemoryDelegate UnmapPhysicalMemory;
public DlPortReadPortUcharDelegate DlPortReadPortUchar;
public DlPortWritePortUcharDelegate DlPortWritePortUchar;
public IsInpOutDriverOpenDelegate IsInpOutDriverOpen;


public InpOut()
{
Expand Down Expand Up @@ -48,6 +50,10 @@ public InpOut()
if (addr == IntPtr.Zero)
throw new ArgumentException("Missing DlPortWritePortUchar");
DlPortWritePortUchar = Marshal.GetDelegateForFunctionPointer<DlPortWritePortUcharDelegate>(addr);
addr = GetProcAddress(libraryHandle, "IsInpOutDriverOpen");
if (addr == IntPtr.Zero)
throw new ArgumentException("Missing IsInpOutDriverOpen");
IsInpOutDriverOpen = Marshal.GetDelegateForFunctionPointer<IsInpOutDriverOpenDelegate>(addr);
}
catch
{
Expand Down Expand Up @@ -95,10 +101,60 @@ public bool WriteMemory(IntPtr baseAddress, byte[] data)
return false;
}

public void ReloadLib()
{
if (libraryHandle != IntPtr.Zero)
{
FreeLibrary(libraryHandle);
libraryHandle = IntPtr.Zero;
}
libraryHandle = LoadLibrary(LibraryName);
if (libraryHandle == IntPtr.Zero)
throw new ArgumentException("Failed to load " + LibraryName);
try
{
var addr = GetProcAddress(libraryHandle, "MapPhysToLin");
if (addr == IntPtr.Zero)
throw new ArgumentException("Missing MapPhysToLin");
MapPhysToLin = Marshal.GetDelegateForFunctionPointer<MapPhysToLinDelegate>(addr);

addr = GetProcAddress(libraryHandle, "UnmapPhysicalMemory");
if (addr == IntPtr.Zero)
throw new ArgumentException("Missing UnmapPhysicalMemory");
UnmapPhysicalMemory = Marshal.GetDelegateForFunctionPointer<UnmapPhysicalMemoryDelegate>(addr);

addr = GetProcAddress(libraryHandle, "UnmapPhysicalMemory");
if (addr == IntPtr.Zero)
throw new ArgumentException("Missing UnmapPhysicalMemory");
UnmapPhysicalMemory = Marshal.GetDelegateForFunctionPointer<UnmapPhysicalMemoryDelegate>(addr);

addr = GetProcAddress(libraryHandle, "DlPortReadPortUchar");
if (addr == IntPtr.Zero)
throw new ArgumentException("Missing DlPortReadPortUchar");
DlPortReadPortUchar = Marshal.GetDelegateForFunctionPointer<DlPortReadPortUcharDelegate>(addr);

addr = GetProcAddress(libraryHandle, "DlPortWritePortUchar");
if (addr == IntPtr.Zero)
throw new ArgumentException("Missing DlPortWritePortUchar");
DlPortWritePortUchar = Marshal.GetDelegateForFunctionPointer<DlPortWritePortUcharDelegate>(addr);
addr = GetProcAddress(libraryHandle, "IsInpOutDriverOpen");
if (addr == IntPtr.Zero)
throw new ArgumentException("Missing IsInpOutDriverOpen");
IsInpOutDriverOpen = Marshal.GetDelegateForFunctionPointer<IsInpOutDriverOpenDelegate>(addr);
}
catch
{
FreeLibrary(libraryHandle);
libraryHandle = IntPtr.Zero;
throw;
}
}

public delegate IntPtr MapPhysToLinDelegate(IntPtr pbPhysAddr, uint dwPhysSize, out IntPtr pPhysicalMemoryHandle);
public delegate bool UnmapPhysicalMemoryDelegate(IntPtr PhysicalMemoryHandle, IntPtr pbLinAddr);
public delegate byte DlPortReadPortUcharDelegate(ushort port);
public delegate byte DlPortWritePortUcharDelegate(ushort port, byte value);
public delegate bool IsInpOutDriverOpenDelegate();

[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr LoadLibrary(string lpFileName);
Expand Down
2 changes: 1 addition & 1 deletion CommonHelpers/Instance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static class Instance
private static bool useKernelDrivers;

private const String GLOBAL_MUTEX_NAME = "Global\\SteamDeckToolsCommonHelpers";
private const int GLOBAL_DEFAULT_TIMEOUT = 10000;
private const int GLOBAL_DEFAULT_TIMEOUT = 20000;

public static bool WantsRunOnStartup
{
Expand Down
9 changes: 9 additions & 0 deletions PowerControl/Helpers/AMD/RyzenSMU.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ public bool Open()
try
{
inpOut = new InpOut();
//inpoutx64 driver may load failed when start with windows after a win11 update, need retry chance
if (!inpOut.IsInpOutDriverOpen())
{
inpOut.ReloadLib();
if (!inpOut.IsInpOutDriverOpen())
{
Log.TraceException("RyzenSMU", new Exception("inpoutx64 driver load failed"));
}
}
mappedAddress = inpOut.MapPhysToLin(MMIO_ADDR, MMIO_SIZE, out physicalHandle);
}
catch (Exception e)
Expand Down

0 comments on commit 31817db

Please sign in to comment.