Skip to content

Commit f2fb284

Browse files
committed
Improve PawnIO discovery and formatting changes
1 parent fed5e99 commit f2fb284

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

cros_ec_python/devices/pawnio.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import sys
66
from ctypes import wintypes
77
from ctypes import util as ctypes_util
8+
import winreg
9+
from typing import Iterable
810

911
from ..baseclass import CrosEcClass
1012
from ..constants.COMMON import *
@@ -28,11 +30,9 @@ def __init__(self, dll: str | None = None, bin: str | None = None):
2830
else:
2931
self.bin = bin or "LpcCrOSEC.bin"
3032

31-
if dll or (dll := ctypes_util.find_library("PawnIOLib.dll")):
32-
self.pawniolib = ctypes.OleDLL(dll)
33-
else:
34-
# Let this raise an error if we can't find it
35-
self.pawniolib = ctypes.OleDLL("C:\\Program Files\\PawnIO\\PawnIOLib.dll")
33+
if not dll:
34+
dll = self._get_location()
35+
self.pawniolib = ctypes.OleDLL(dll)
3636

3737
self.pawniolib.pawnio_version.argtypes = [ctypes.POINTER(wintypes.ULONG)]
3838
self.pawniolib.pawnio_open.argtypes = [ctypes.POINTER(wintypes.HANDLE)]
@@ -81,8 +81,8 @@ def _pawnio_load(self, filepath: str) -> None:
8181
def _pawnio_execute(
8282
self,
8383
function: str,
84-
in_data: bytes,
85-
out_size: bytes,
84+
in_data: Iterable,
85+
out_size: int,
8686
in_size: int | None = None,
8787
) -> tuple[int | ctypes.Array]:
8888
function_bytes = function.encode("utf-8")
@@ -106,14 +106,29 @@ def _pawnio_execute(
106106
def _pawnio_close(self):
107107
self.pawniolib.pawnio_close(self.handle)
108108

109+
@staticmethod
110+
def _get_location() -> str | None:
111+
"""
112+
Get the location of the PawnIO driver.
113+
"""
114+
try:
115+
with winreg.OpenKey(
116+
winreg.HKEY_LOCAL_MACHINE,
117+
r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PawnIO"
118+
) as key:
119+
return winreg.QueryValueEx(key, "InstallLocation")[0] + r"\\PawnIOLib.dll"
120+
except FileNotFoundError:
121+
fallback = os.environ.get("ProgramFiles", r"C:\Program Files") + r"\PawnIO\PawnIOLib.dll"
122+
if os.path.exists(fallback):
123+
return fallback
124+
return None
125+
109126
@staticmethod
110127
def detect() -> bool:
111128
"""
112129
Detect if the PawnIO driver is installed.
113130
"""
114-
return bool(ctypes_util.find_library("PawnIOLib.dll")) or os.path.exists(
115-
"C:\\Program Files\\PawnIO\\PawnIOLib.dll"
116-
)
131+
return CrosEcPawnIO._get_location() is not None
117132

118133
def ec_init(self) -> None:
119134
self._pawnio_open()

cros_ec_python/devices/win_fw_ec.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,31 @@
2929
INVALID_HANDLE_VALUE: Final = -1
3030

3131

32-
def ctl_code(device_type, function, method, access) -> int:
33-
return ((device_type) << 16) + ((access) << 14) + ((function) << 2) + method
32+
def CTL_CODE(device_type, function, method, access) -> int:
33+
return (device_type << 16) | (access << 14) | (function << 2) | method
3434

3535

3636
CROSEC_CMD_MAX_REQUEST = 0x100
3737

3838
FILE_DEVICE_CROS_EMBEDDED_CONTROLLER = 0x80EC
3939

40-
IOCTL_CROSEC_XCMD = ctl_code(
40+
IOCTL_CROSEC_XCMD = CTL_CODE(
4141
FILE_DEVICE_CROS_EMBEDDED_CONTROLLER,
4242
0x801,
4343
METHOD_BUFFERED,
4444
FILE_READ_DATA | FILE_WRITE_DATA,
4545
)
46-
IOCTL_CROSEC_RDMEM = ctl_code(
46+
IOCTL_CROSEC_RDMEM = CTL_CODE(
4747
FILE_DEVICE_CROS_EMBEDDED_CONTROLLER,
4848
0x802,
4949
METHOD_BUFFERED,
5050
FILE_READ_ACCESS,
5151
)
5252

5353

54-
def CreateFileW(filename, access, mode, creation, flags) -> wintypes.HANDLE:
54+
def CreateFileW(
55+
filename: str, access: int, mode: int, creation: int, flags: int
56+
) -> wintypes.HANDLE:
5557
knl32_CreateFileW = windll.kernel32.CreateFileW
5658
knl32_CreateFileW.argtypes = [
5759
wintypes.LPCWSTR, # lpFileName
@@ -69,7 +71,14 @@ def CreateFileW(filename, access, mode, creation, flags) -> wintypes.HANDLE:
6971
)
7072

7173

72-
def DeviceIoControl(devhandle, ioctl, inbuf, inbufsiz, outbuf, outbufsiz) -> bool:
74+
def DeviceIoControl(
75+
handle: wintypes.HANDLE,
76+
ioctl: int,
77+
inbuf: ctypes.pointer,
78+
insize: int,
79+
outbuf: ctypes.pointer,
80+
outsize,
81+
) -> bool:
7382
knl32_DeviceIoControl = windll.kernel32.DeviceIoControl
7483
knl32_DeviceIoControl.argtypes = [
7584
wintypes.HANDLE, # hDevice
@@ -84,7 +93,7 @@ def DeviceIoControl(devhandle, ioctl, inbuf, inbufsiz, outbuf, outbufsiz) -> boo
8493
knl32_DeviceIoControl.restype = wintypes.BOOL
8594

8695
status = knl32_DeviceIoControl(
87-
devhandle, ioctl, inbuf, inbufsiz, outbuf, outbufsiz, None, None
96+
handle, ioctl, inbuf, insize, outbuf, outsize, None, None
8897
)
8998

9099
return bool(status)

0 commit comments

Comments
 (0)