Skip to content

Commit 849305c

Browse files
committed
Improve PawnIO discovery and formatting changes
Also fixes for pdoc
1 parent fed5e99 commit 849305c

File tree

2 files changed

+44
-17
lines changed

2 files changed

+44
-17
lines changed

cros_ec_python/devices/pawnio.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
import sys
66
from ctypes import wintypes
77
from ctypes import util as ctypes_util
8+
from typing import Iterable
9+
10+
# Workaround for pdoc failing on Linux
11+
if os.name == 'nt':
12+
import winreg
813

914
from ..baseclass import CrosEcClass
1015
from ..constants.COMMON import *
@@ -28,11 +33,9 @@ def __init__(self, dll: str | None = None, bin: str | None = None):
2833
else:
2934
self.bin = bin or "LpcCrOSEC.bin"
3035

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")
36+
if not dll:
37+
dll = self._get_location()
38+
self.pawniolib = ctypes.OleDLL(dll)
3639

3740
self.pawniolib.pawnio_version.argtypes = [ctypes.POINTER(wintypes.ULONG)]
3841
self.pawniolib.pawnio_open.argtypes = [ctypes.POINTER(wintypes.HANDLE)]
@@ -81,8 +84,8 @@ def _pawnio_load(self, filepath: str) -> None:
8184
def _pawnio_execute(
8285
self,
8386
function: str,
84-
in_data: bytes,
85-
out_size: bytes,
87+
in_data: Iterable,
88+
out_size: int,
8689
in_size: int | None = None,
8790
) -> tuple[int | ctypes.Array]:
8891
function_bytes = function.encode("utf-8")
@@ -106,14 +109,29 @@ def _pawnio_execute(
106109
def _pawnio_close(self):
107110
self.pawniolib.pawnio_close(self.handle)
108111

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

118136
def ec_init(self) -> None:
119137
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: int,
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)