|
| 1 | +""" |
| 2 | +This file provides a way to interact with the WinRing0 library for port I/O on Windows. |
| 3 | +
|
| 4 | +For this to work, you will need to copy `WinRing0x64.dll` and `WinRing0x64.sys` to either |
| 5 | +the same directory as `python.exe`, or to `C:\Windows\System32`. |
| 6 | +""" |
| 7 | + |
| 8 | +from enum import Enum |
| 9 | +import ctypes |
| 10 | + |
| 11 | +from .baseportio import PortIOClass |
| 12 | + |
| 13 | + |
| 14 | +class WinPortIO(PortIOClass): |
| 15 | + """ |
| 16 | + A class to interact with the WinRing0 library for port I/O on Windows. |
| 17 | + """ |
| 18 | + |
| 19 | + winring0 = None |
| 20 | + |
| 21 | + def __init__(self): |
| 22 | + """ |
| 23 | + Load and initialize the WinRing0 library. |
| 24 | + """ |
| 25 | + self.winring0 = ctypes.WinDLL("WinRing0x64.dll") |
| 26 | + self.winring0.InitializeOls.restype = ctypes.c_bool |
| 27 | + self.winring0.GetDllStatus.restype = ctypes.c_ulong |
| 28 | + self.winring0.DeinitializeOls.restype = None |
| 29 | + # ReadIoPort (port) |
| 30 | + self.winring0.ReadIoPortByte.restype = ctypes.c_ubyte |
| 31 | + self.winring0.ReadIoPortByte.argtypes = [ctypes.c_ushort] |
| 32 | + self.winring0.ReadIoPortWord.restype = ctypes.c_ushort |
| 33 | + self.winring0.ReadIoPortWord.argtypes = [ctypes.c_ushort] |
| 34 | + self.winring0.ReadIoPortDword.restype = ctypes.c_ulong |
| 35 | + self.winring0.ReadIoPortDword.argtypes = [ctypes.c_ushort] |
| 36 | + # WriteIoPort (port, data) |
| 37 | + self.winring0.WriteIoPortByte.argtypes = [ctypes.c_ushort, ctypes.c_ubyte] |
| 38 | + self.winring0.WriteIoPortWord.argtypes = [ctypes.c_ushort, ctypes.c_ushort] |
| 39 | + self.winring0.WriteIoPortDword.argtypes = [ctypes.c_ushort, ctypes.c_ulong] |
| 40 | + |
| 41 | + self.winring0.InitializeOls() |
| 42 | + if error := self.winring0.GetDllStatus(): |
| 43 | + raise OSError(f"WinRing0 Error: {self.Status(error)} ({error})") |
| 44 | + |
| 45 | + def __del__(self): |
| 46 | + """ |
| 47 | + Deinitialize the WinRing0 library. |
| 48 | + """ |
| 49 | + if self.winring0: |
| 50 | + self.winring0.DeinitializeOls() |
| 51 | + |
| 52 | + class Status(Enum): |
| 53 | + OLS_DLL_NO_ERROR = 0 |
| 54 | + OLS_DLL_UNSUPPORTED_PLATFORM = 1 |
| 55 | + OLS_DLL_DRIVER_NOT_LOADED = 2 |
| 56 | + OLS_DLL_DRIVER_NOT_FOUND = 3 |
| 57 | + OLS_DLL_DRIVER_UNLOADED = 4 |
| 58 | + OLS_DLL_DRIVER_NOT_LOADED_ON_NETWORK = 5 |
| 59 | + OLS_DLL_UNKNOWN_ERROR = 9 |
| 60 | + |
| 61 | + OLS_DLL_DRIVER_INVALID_PARAM = 10 |
| 62 | + OLS_DLL_DRIVER_SC_MANAGER_NOT_OPENED = 11 |
| 63 | + OLS_DLL_DRIVER_SC_DRIVER_NOT_INSTALLED = 12 |
| 64 | + OLS_DLL_DRIVER_SC_DRIVER_NOT_STARTED = 13 |
| 65 | + OLS_DLL_DRIVER_SC_DRIVER_NOT_REMOVED = 14 |
| 66 | + |
| 67 | + def outb(self, data: int, port: int) -> None: |
| 68 | + """ |
| 69 | + Write a byte (8 bit) to the specified port. |
| 70 | + :param data: Byte to write. |
| 71 | + :param port: Port to write to. |
| 72 | + """ |
| 73 | + self.winring0.WriteIoPortByte(port, data) |
| 74 | + |
| 75 | + def outw(self, data: int, port: int) -> None: |
| 76 | + """ |
| 77 | + Write a word (16 bit) to the specified port. |
| 78 | + :param data: Word to write. |
| 79 | + :param port: Port to write to. |
| 80 | + """ |
| 81 | + self.winring0.WriteIoPortWord(port, data) |
| 82 | + |
| 83 | + def outl(self, data: int, port: int) -> None: |
| 84 | + """ |
| 85 | + Write a long (32 bit) to the specified port. |
| 86 | + :param data: Long to write. |
| 87 | + :param port: Port to write to. |
| 88 | + """ |
| 89 | + self.winring0.WriteIoPortDword(port, data) |
| 90 | + |
| 91 | + def inb(self, port: int) -> int: |
| 92 | + """ |
| 93 | + Read a byte (8 bit) from the specified port. |
| 94 | + :param port: Port to read from. |
| 95 | + :return: Byte read. |
| 96 | + """ |
| 97 | + return self.winring0.ReadIoPortByte(port) |
| 98 | + |
| 99 | + def inw(self, port: int) -> int: |
| 100 | + """ |
| 101 | + Read a word (16 bit) from the specified port. |
| 102 | + :param port: Port to read from. |
| 103 | + :return: Word read. |
| 104 | + """ |
| 105 | + return self.winring0.ReadIoPortWord(port) |
| 106 | + |
| 107 | + def inl(self, port: int) -> int: |
| 108 | + """ |
| 109 | + Read a long (32 bit) from the specified port. |
| 110 | + :param port: Port to read from. |
| 111 | + :return: Long read. |
| 112 | + """ |
| 113 | + return self.winring0.ReadIoPortDword(port) |
| 114 | + |
| 115 | + def ioperm(self, port: int, num: int, turn_on: bool) -> None: |
| 116 | + """ |
| 117 | + `ioperm` stub function. It's not required for WinRing0. |
| 118 | + """ |
| 119 | + pass |
| 120 | + |
| 121 | + def iopl(self, level: int) -> None: |
| 122 | + """ |
| 123 | + `iopl` stub function. It's not required for WinRing0. |
| 124 | + """ |
| 125 | + pass |
0 commit comments