44import os
55import sys
66from ctypes import wintypes
7- from ctypes import util as ctypes_util
87from typing import Iterable
98
109# Workaround for pdoc failing on Linux
@@ -23,7 +22,7 @@ class CrosEcPawnIO(CrosEcClass):
2322
2423 def __init__ (self , dll : str | None = None , bin : str | None = None ):
2524 """
26- Initialise the EC using the Linux cros_ec device .
25+ Initialise the EC using the PawnIO LpcCrOSEC driver .
2726 :param dll: Path to the DLL to use. If None, will use the default path.
2827 :param bin: Path to the binary to load. If None, will use the default path.
2928 """
@@ -90,8 +89,8 @@ def _pawnio_execute(
9089 ) -> tuple [int | ctypes .Array ]:
9190 function_bytes = function .encode ("utf-8" )
9291 in_size = in_size if in_size is not None else len (in_data )
93- in_array = (ctypes .c_ulonglong * in_size )(* in_data )
94- out_array = (ctypes .c_ulonglong * out_size )()
92+ in_array = (ctypes .c_uint64 * in_size )(* in_data )
93+ out_array = (ctypes .c_uint64 * out_size )()
9594 return_size = ctypes .c_size_t ()
9695
9796 self .pawniolib .pawnio_execute (
@@ -163,19 +162,28 @@ def command(
163162 :param warn: Whether to warn if the response size is not as expected. Default is True.
164163 :return: Incoming data from EC.
165164 """
166- # LpcCrOSEC returns the EC result too
167- pawn_insize = insize + 1
168- header = struct .pack ("<HB" , command , version )
165+
166+ pawn_in_data = [version , command , outsize , insize ]
167+ # Convert data to 64-bit cells
168+ for i in range (0 , len (data or ()), 8 ):
169+ if i + 8 > len (data ):
170+ # If the data is not a multiple of 8, pad it with zeros
171+ pawn_in_data .append (struct .unpack ('<Q' , data [i :i + 8 ] + b'\x00 ' * (8 - len (data [i :i + 8 ])))[0 ])
172+ else :
173+ pawn_in_data .append (struct .unpack ('<Q' , data [i :i + 8 ])[0 ])
174+
175+ # Convert insize from bytes to 64bit cells, + 1 for the EC result
176+ pawn_out_size = - (- insize // 8 ) + 1
177+
169178 size , res = self ._pawnio_execute (
170179 "ioctl_ec_command" ,
171- header + (data or b"" ),
172- pawn_insize ,
173- in_size = outsize + len (header ),
180+ pawn_in_data ,
181+ pawn_out_size
174182 )
175183
176- if size != pawn_insize and warn :
184+ if size != pawn_out_size and warn :
177185 warnings .warn (
178- f"Expected { pawn_insize } bytes, got { size } back from PawnIO" ,
186+ f"Expected { pawn_out_size } bytes, got { size } back from PawnIO" ,
179187 RuntimeWarning ,
180188 )
181189
@@ -188,12 +196,10 @@ def command(
188196 # Otherwise it's the length
189197 if res [0 ] != insize and warn :
190198 warnings .warn (
191- f"Expected { pawn_insize } bytes, got { res [0 ]} back from EC" ,
199+ f"Expected { pawn_out_size } bytes, got { res [0 ]} back from EC" ,
192200 RuntimeWarning ,
193201 )
194-
195- # The pawn cells are 64bit but all the values should fit in 8 bits
196- return bytes (res [1 :])
202+ return bytes (res )[8 :insize + 8 ]
197203
198204 def memmap (self , offset : Int32 , num_bytes : Int32 ) -> bytes :
199205 """
@@ -203,10 +209,6 @@ def memmap(self, offset: Int32, num_bytes: Int32) -> bytes:
203209 :return: Bytes read from the EC.
204210 """
205211 size , res = self ._pawnio_execute (
206- "ioctl_ec_readmem" , offset . to_bytes ( 1 ), num_bytes
212+ "ioctl_ec_readmem" , ( offset , num_bytes ), - ( - num_bytes // 8 )
207213 )
208-
209- array = bytearray (size )
210- for i in range (size ):
211- array [i ] = res [i ]
212- return bytes (array )
214+ return bytes (res )[:num_bytes ]
0 commit comments