| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537 |
- diff --git a/libtudor/meson.build b/libtudor/meson.build
- index d2556af..0854792 100644
- --- a/libtudor/meson.build
- +++ b/libtudor/meson.build
- @@ -74,23 +74,26 @@ driver_dll_names = [
-
- add_global_arguments('-Wno-incompatible-pointer-types', language : 'c')
- add_global_arguments('-g', language : ['c', 'cpp'])
- -add_global_arguments('-fsanitize=address', language : ['c', 'cpp'])
- +# add_global_arguments('-fsanitize=address', language : ['c', 'cpp'])
-
-
- bridge_proj = subproject('cryptbridge')
- cryptbridge_dep = bridge_proj.get_variable('cryptbridge_dep')
-
-
- -driver_dlls = custom_target('driver-download',
- - output: driver_dll_names,
- - input: ['download_driver.sh', 'installer.sha'],
- - command: [find_program('bash'), '@INPUT0@', '@INPUT1@', '@PRIVATE_DIR@', '@OUTDIR@'] + driver_dll_names
- -)
- -
- -driver_dll_blobs = generator(find_program('ld'),
- - output: '@BASENAME@.o',
- - arguments: ['-r', '-b', 'binary', '-o', '@OUTPUT@', '@INPUT@']
- -).process([driver_dlls])
- +# Local build: use pre-extracted Synaptics 5.5.2731.1050 DLLs instead of
- +# the original download_driver.sh (which was hardcoded to the author's machine).
- +driver_dlls = []
- +foreach n : driver_dll_names
- + driver_dlls += custom_target('blob-' + n,
- + output: n + '.o',
- + input: files('/home/nebulon/projects/fingerprint/driverdlls/' + n),
- + command: ['sh', '-c',
- + 'mkdir -p libtudor && cp "$1" "libtudor/$2" && exec ld -r -b binary -o "$3" "libtudor/$2"',
- + 'sh', '@INPUT@', n, '@OUTPUT@'],
- + build_by_default: true,
- + )
- +endforeach
-
- libtudor_flags = []
-
- @@ -110,7 +113,7 @@ libtudor_inc = include_directories('inc')
- # wudm1_inc = include_directories('src/wudm1_inc')
- # winapi_inc = include_directories('src/winapi')
-
- -libtudor = shared_library('tudor', [libtudor_src, driver_dll_blobs],
- +libtudor = shared_library('tudor', [libtudor_src, driver_dlls],
- dependencies: [thread_dep, libcrypto_dep, libusb_dep, cryptbridge_dep],
- c_args: ['-D_GNU_SOURCE'] + libtudor_flags,
- cpp_args: ['-D_GNU_SOURCE'] + libtudor_flags,
- diff --git a/libtudor/src/loader.c b/libtudor/src/loader.c
- index be29477..4402170 100644
- --- a/libtudor/src/loader.c
- +++ b/libtudor/src/loader.c
- @@ -67,6 +67,8 @@ bool load_dll(struct dll_image *dll, const char *name, uint8_t *data, uint32_t s
- //Copy data
- pe_copy_mem(&pe, 0, pe.image_size, image_mem);
- log_debug("Copied image memory to mapping at %p - %p", image_mem, image_mem + pe.image_size);
- + //Machine-readable load line for post-mortem crash symbolization
- + fprintf(stderr, "TUDOR_LOAD %s base=%p size=%x\n", name, image_mem, pe.image_size);
-
- //Resolve imports
- for(int i = 0; i < pe.num_import_libs; i++) {
- diff --git a/libtudor/src/tudor/driver.c b/libtudor/src/tudor/driver.c
- index 156c9de..63a306d 100644
- --- a/libtudor/src/tudor/driver.c
- +++ b/libtudor/src/tudor/driver.c
- @@ -40,7 +40,7 @@ static struct winmodule ntdll_module = {
- typedef BOOL __winfnc (*api_DllMain)(HANDLE hinstDLL, int fdwReason, void *lpReserved);
-
- struct windrv_dll *tudor_adapter_dll, *tudor_driver_dll;
- -WINBIO_SENSOR_INTERFACE *tudor_sensor_adapter;
- +WINBIO_SENSOR_INTERFACE_V1 *tudor_sensor_adapter;
- WINBIO_ENGINE_INTERFACE *tudor_engine_adapter;
-
- static DRIVER_OBJECT umdf_driver;
- @@ -126,10 +126,34 @@ bool tudor_init() {
- winmodule_set_cur(&tudor_adapter_dll->module);
-
- HRESULT hres;
- - if((hres = ((api_WbioQuerySensorInterface) find_dll_export(&tudor_adapter_dll->image, "WbioQuerySensorInterface"))(&tudor_sensor_adapter)) != 0) {
- + if((hres = ((api_WbioQuerySensorInterface) find_dll_export(&tudor_adapter_dll->image, "WbioQuerySensorInterface"))((WINBIO_SENSOR_INTERFACE**) &tudor_sensor_adapter)) != 0) {
- log_error("Error querying sensor interface: 0x%x!", hres);
- return false;
- }
- + //Snapshot the sensor interface table, then restore every function slot
- + //from the pristine static image (the relinked adapter's runtime state
- + //overlaps the table's static storage in .data and clobbers slots).
- + {
- + WINBIO_SENSOR_INTERFACE_V1 *snapshot = (WINBIO_SENSOR_INTERFACE_V1*) malloc(sizeof(WINBIO_SENSOR_INTERFACE_V1));
- + memcpy(snapshot, tudor_sensor_adapter, sizeof(WINBIO_SENSOR_INTERFACE_V1));
- + tudor_sensor_adapter = snapshot;
- +
- + uintptr_t mod_base = (uintptr_t) tudor_adapter_dll->image.base_addr;
- + static const uint32_t sensor_slot_rvas[15] = {
- + 0x005b50, 0x005d50, 0x005ee0, 0x005ff0, 0x006250,
- + 0x006460, 0x0065d0, 0x006660, 0x0066f0, 0x006bf0,
- + 0x006ec0, 0x006f50, 0x0070b0, 0x0072f0, 0x0075b0,
- + };
- + void **slots = (void**) tudor_sensor_adapter;
- + for(int i = 0; i < 15; i++) {
- + slots[4 + i] = (void*) (mod_base + sensor_slot_rvas[i]);
- + }
- + //Alias the Windows 10 names onto the legacy entry points.
- + tudor_sensor_adapter->PipelineInit = (IBIO_SENSOR_PIPELINE_INIT_FN*) tudor_sensor_adapter->Reset;
- + tudor_sensor_adapter->PipelineCleanup = (IBIO_SENSOR_PIPELINE_CLEANUP_FN*) tudor_sensor_adapter->Cancel;
- + tudor_sensor_adapter->Activate = (IBIO_SENSOR_ACTIVATE_FN*) tudor_sensor_adapter->Reset;
- + tudor_sensor_adapter->Deactivate = (IBIO_SENSOR_DEACTIVATE_FN*) tudor_sensor_adapter->Cancel;
- + }
- if((hres = ((api_WbioQueryEngineInterface) find_dll_export(&tudor_adapter_dll->image, "WbioQueryEngineInterface"))(&tudor_engine_adapter)) != 0) {
- log_error("Error querying engine interface: 0x%x!", hres);
- return false;
- diff --git a/libtudor/src/tudor/driver_wudf1.cpp b/libtudor/src/tudor/driver_wudf1.cpp
- index dde101b..bcc5f1c 100644
- --- a/libtudor/src/tudor/driver_wudf1.cpp
- +++ b/libtudor/src/tudor/driver_wudf1.cpp
- @@ -50,6 +50,10 @@ static winmodule ntdll_module = {
- .environ = (const char*[]) { NULL }
- };
-
- +//WUDF call tracing for debugging: compile-in, enabled via env TUDOR_WUDF_TRACE
- +static bool tudor_wudf_trace = (getenv("TUDOR_WUDF_TRACE") != NULL);
- +#define WUDF_TRACE(...) do { if(tudor_wudf_trace) { printf("[WUDF] " __VA_ARGS__); fflush(stdout); } } while(0)
- +
- #define DLL_PROCESS_ATTACH 1
- #define DLL_PROCESS_DETACH 0
- #define DLL_THREAD_ATTACH 2
- @@ -57,9 +61,16 @@ static winmodule ntdll_module = {
- typedef BOOL __winfnc (*api_DllMain)(HANDLE hinstDLL, int fdwReason, void *lpReserved);
-
- struct windrv_dll *tudor_adapter_dll, *tudor_driver_dll;
- -WINBIO_SENSOR_INTERFACE *tudor_sensor_adapter;
- +WINBIO_SENSOR_INTERFACE_V1 *tudor_sensor_adapter;
- WINBIO_ENGINE_INTERFACE *tudor_engine_adapter;
-
- +//Dummy engine adapter used with TUDOR_NO_ENGINE (for sensor bring-up tests)
- +static HRESULT dummy_engine_attach(WINBIO_PIPELINE *pipeline) { (void) pipeline; return ERROR_SUCCESS; }
- +static HRESULT dummy_engine_pipeline_init(WINBIO_PIPELINE *pipeline) { (void) pipeline; return ERROR_SUCCESS; }
- +static HRESULT dummy_engine_pipeline_cleanup(WINBIO_PIPELINE *pipeline) { (void) pipeline; return ERROR_SUCCESS; }
- +static HRESULT dummy_engine_activate(WINBIO_PIPELINE *pipeline) { (void) pipeline; return ERROR_SUCCESS; }
- +static HRESULT dummy_engine_deactivate(WINBIO_PIPELINE *pipeline) { (void) pipeline; return ERROR_SUCCESS; }
- +
- static DRIVER_OBJECT umdf_driver;
- struct winwdf_driver *tudor_wdf_driver;
-
- @@ -89,7 +100,6 @@ struct windrv_dll tudor_windrv_dlls[] = {
- };
-
-
- -
- #define DEFINE_GUID11(name, a, b, c, d, e, f, g, h, i, j, k) \
- static const GUID_DLL name = { \
- (a), (b), (c), \
- @@ -209,7 +219,8 @@ struct IObjectCleanup;
- class MyDevInit : public IWDFDeviceInitialize {
- public:
- virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject) {
- - printf("QueryInterface\r\n");
- + LPOLESTR _s; StringFromIID(riid, &_s);
- + std::wcout << L"[QI] " << _s << (ppvObject ? (void*)*ppvObject : (void*)0) << std::endl;
- return 0;
- }
- virtual ULONG STDMETHODCALLTYPE AddRef(void) {
- @@ -241,6 +252,9 @@ class MyDevInit : public IWDFDeviceInitialize {
- _Out_ IWDFNamedPropertyStore **ppPropStore,
- /* [annotation][unique][out] */
- _Out_opt_ WDF_PROPERTY_STORE_DISPOSITION *pDisposition) {
- + if(ppPropStore) *ppPropStore = NULL;
- + if(pDisposition) *pDisposition = (WDF_PROPERTY_STORE_DISPOSITION) 0;
- +
- printf("RetrieveDevicePropertyStore\r\n");
- return 0;
- }
- @@ -304,31 +318,39 @@ struct MyNamedPropertyStore : public IWDFNamedPropertyStore2 {
- public:
-
- virtual HRESULT STDMETHODCALLTYPE DeleteWdfObject( void) {
- + printf("[MEM] DeleteWdfObject\n"); fflush(stdout);
- printf("DeleteWdfObject\r\n");
- return 0;
- }
-
- + void *assigned_context = NULL;
- virtual HRESULT STDMETHODCALLTYPE AssignContext(
- /* [annotation][unique][in] */
- _In_opt_ __drv_aliasesMem IObjectCleanup *pCleanupCallback,
- /* [annotation][unique][in] */
- - _In_opt_ __drv_aliasesMem void *pContext) {
- - printf("AssignContext\r\n");
- + _In_opt_ __drv_aliasesMem void *pContext) {
- + printf("[MEM] AssignContext\n"); fflush(stdout);
- + printf("AssignContext(%p)\r\n", pContext);
- + assigned_context = pContext;
- return 0;
- }
-
- virtual HRESULT STDMETHODCALLTYPE RetrieveContext(
- /* [annotation][out] */
- _Out_ void **ppvContext) {
- - printf("RetrieveContext\r\n");
- + printf("[MEM] RetrieveContext\n"); fflush(stdout);
- + if(ppvContext) *ppvContext = assigned_context;
- + printf("RetrieveContext -> %p\r\n", assigned_context);
- return 0;
- }
-
- virtual void STDMETHODCALLTYPE AcquireLock( void) {
- + printf("[MEM] AcquireLock\n"); fflush(stdout);
- printf("AcquireLock\r\n");
- }
-
- virtual void STDMETHODCALLTYPE ReleaseLock( void) {
- + printf("[MEM] ReleaseLock\n"); fflush(stdout);
- printf("ReleaseLock\r\n");
- }
- public:
- @@ -338,7 +360,7 @@ struct MyNamedPropertyStore : public IWDFNamedPropertyStore2 {
- /* [annotation][out] */
- _Out_ PROPVARIANT *pv){
- std::wcout << L"=====================================" << std::endl;
- - std::wcout << L"GetNamedValue " << pszName << std::endl;
- + std::cout << "GetNamedValue " << utf16le_to_utf8(pszName) << std::endl;
- std::string fname = utf16le_to_utf8(pszName);
- std::cout << fname << std::endl;
- std::wcout << L"=====================================" << std::endl;
- @@ -348,6 +370,11 @@ struct MyNamedPropertyStore : public IWDFNamedPropertyStore2 {
- std::vector<char> buf((std::istreambuf_iterator<char>(input)), std::istreambuf_iterator<char>());
- std::cout << "Loaded " << buf.size() << " bytes of calibration data" << std::endl;
- if(buf.size() == 0) {
- + //No stored calibration data yet: report an empty value.
- + //The vt tag MUST be initialized, the caller passes an
- + //uninitialized PROPVARIANT buffer.
- + memset(pv, 0, sizeof(*pv));
- + pv->vt = VT_DECIMAL;
- DECIMAL_SETZERO(pv->decVal);
- } else {
- pv->vt = VT_BLOB;
- @@ -365,7 +392,9 @@ struct MyNamedPropertyStore : public IWDFNamedPropertyStore2 {
- std::cout << "UINT " << fname << " = " << pv->uintVal << std::endl;
- }
- else {
- - DECIMAL_SETZERO(pv->decVal);
- + memset(pv, 0, sizeof(*pv));
- + pv->vt = VT_UINT;
- + pv->uintVal = 0;
- std::cout << "UINT " << fname << " not found" << std::endl;
- }
- }
- @@ -389,7 +418,7 @@ struct MyNamedPropertyStore : public IWDFNamedPropertyStore2 {
- //PropVariantToStringAlloc(*pv, &buf);
-
- std::wcout << L"=====================================" << std::endl;
- - std::wcout << L"SetNamedValue " << pszName << "=" << pv->vt << std::endl;
- + std::cout << "SetNamedValue " << utf16le_to_utf8(pszName) << "=" << pv->vt << std::endl;
- std::string str_u8 = utf16le_to_utf8(pszName);
- std::cout << str_u8 << std::endl;
- switch(pv->vt) {
- @@ -447,13 +476,14 @@ struct MyNamedPropertyStore : public IWDFNamedPropertyStore2 {
- virtual HRESULT STDMETHODCALLTYPE DeleteNamedValue(
- /* [annotation][string][in] */
- _In_ LPCWSTR pwszName){
- - std::wcout << L"DeleteNamedValue " << pwszName<< std::endl;
- + std::cout << "DeleteNamedValue " << utf16le_to_utf8(pwszName) << std::endl;
- return 0;
- }
-
- };
-
- struct MyMem : public IWDFMemory {
- + void *assigned_context = NULL;
- public:
- MyMem(void *b, SIZE_T s) {
- buf = b;
- @@ -489,14 +519,16 @@ struct MyMem : public IWDFMemory {
- _In_opt_ __drv_aliasesMem void *pCleanupCallback,
- /* [annotation][unique][in] */
- _In_opt_ __drv_aliasesMem void *pContext) {
- - printf("AssignContext\r\n");
- + assigned_context = pContext;
- + printf("AssignContext(%p)\r\n", pContext);
- return 0;
- }
-
- virtual HRESULT STDMETHODCALLTYPE RetrieveContext(
- /* [annotation][out] */
- _Out_ void **ppvContext) {
- - printf("RetrieveContext\r\n");
- + if(ppvContext) *ppvContext = assigned_context;
- + printf("RetrieveContext -> %p\r\n", assigned_context);
- return 0;
- }
-
- @@ -513,6 +545,7 @@ struct MyMem : public IWDFMemory {
- _In_ IWDFMemory *Source,
- /* [annotation][unique][in] */
- _In_opt_ PWDFMEMORY_OFFSET SourceOffset){
- + printf("[MEM] CopyFromMemory\n"); fflush(stdout);
- printf("CopyFromMemory\r\n");
- return 0;
- }
- @@ -525,6 +558,7 @@ struct MyMem : public IWDFMemory {
- void *TargetBuffer,
- /* [annotation][in] */
- _In_ SIZE_T NumOfBytesToCopyTo){
- + printf("[MEM] CopyToBuffer\n"); fflush(stdout);
- printf("CopyToBuffer\r\n");
- return 0;
- }
- @@ -537,12 +571,14 @@ struct MyMem : public IWDFMemory {
- void *SourceBuffer,
- /* [annotation][in] */
- _In_ SIZE_T NumOfBytesToCopyFrom){
- + printf("[MEM] CopyFromBuffer\n"); fflush(stdout);
- printf("CopyFromBuffer\r\n");
- return 0;
- }
-
-
- virtual SIZE_T STDMETHODCALLTYPE GetSize( void){
- + printf("[MEM] GetSize\n"); fflush(stdout);
- printf("GetSize\r\n");
- return size;
- }
- @@ -551,6 +587,7 @@ struct MyMem : public IWDFMemory {
- virtual void *STDMETHODCALLTYPE GetDataBuffer(
- /* [annotation][unique][out] */
- _Out_opt_ SIZE_T *BufferSize){
- + printf("[MEM] GetDataBuffer\n"); fflush(stdout);
- if(BufferSize) {
- *BufferSize = size;
- }
- @@ -564,6 +601,7 @@ struct MyMem : public IWDFMemory {
- void *Buffer,
- /* [annotation][in] */
- _In_ SIZE_T BufferSize){
- + printf("[MEM] SetBuffer\n"); fflush(stdout);
- printf("SetBuffer\r\n");
- buf = Buffer;
- size = BufferSize;
- @@ -574,6 +612,7 @@ struct MyMem : public IWDFMemory {
- };
-
- struct MyRequest : public IWDFIoRequest {
- + void *assigned_context = NULL;
- public:
- MyRequest(WDF_REQUEST_TYPE t, ULONG c, MyMem *out, MyMem *in) {
- reqType = t;
- @@ -582,12 +621,14 @@ struct MyRequest : public IWDFIoRequest {
- inMem = in;
- complete = FALSE;
- informationSize = 0;
- + completionStatus = 0;
- }
-
- WDF_REQUEST_TYPE reqType;
- ULONG ctl;
- BOOL complete;
- LONG_PTR informationSize;
- + HRESULT completionStatus;
- public:
- virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject) {
- LPOLESTR str;
- @@ -617,14 +658,16 @@ struct MyRequest : public IWDFIoRequest {
- _In_opt_ __drv_aliasesMem void *pCleanupCallback,
- /* [annotation][unique][in] */
- _In_opt_ __drv_aliasesMem void *pContext) {
- - printf("AssignContext\r\n");
- + assigned_context = pContext;
- + printf("AssignContext(%p)\r\n", pContext);
- return 0;
- }
-
- virtual HRESULT STDMETHODCALLTYPE RetrieveContext(
- /* [annotation][out] */
- _Out_ void **ppvContext) {
- - printf("RetrieveContext\r\n");
- + if(ppvContext) *ppvContext = assigned_context;
- + printf("RetrieveContext -> %p\r\n", assigned_context);
- return 0;
- }
-
- @@ -641,7 +684,9 @@ struct MyRequest : public IWDFIoRequest {
- _In_ HRESULT CompletionStatus,
- /* [annotation][in] */
- _In_ SIZE_T Information){
- + printf("[REQ] CompleteWithInformation\n"); fflush(stdout);
- printf("CompleteWithInformation\r\n");
- + completionStatus = CompletionStatus;
- complete = TRUE;
- }
-
- @@ -649,6 +694,7 @@ struct MyRequest : public IWDFIoRequest {
- virtual void STDMETHODCALLTYPE SetInformation(
- /* [annotation][in] */
- _In_ ULONG_PTR Information){
- + printf("[REQ] SetInformation\n"); fflush(stdout);
- printf("SetInformation size=%lld\r\n", Information);
- informationSize = Information;
- }
- @@ -657,7 +703,9 @@ struct MyRequest : public IWDFIoRequest {
- virtual void STDMETHODCALLTYPE Complete(
- /* [annotation][in] */
- _In_ HRESULT CompletionStatus){
- + printf("[REQ] Complete\n"); fflush(stdout);
- printf("Complete: %lx\r\n", (unsigned long)CompletionStatus);
- + completionStatus = CompletionStatus;
- complete = TRUE;
- }
-
- @@ -667,11 +715,13 @@ struct MyRequest : public IWDFIoRequest {
- _In_ void *pCompletionCallback,
- /* [annotation][unique][in] */
- _In_opt_ void *pContext){
- + printf("[REQ] SetCompletionCallback\n"); fflush(stdout);
- printf("SetCompletionCallback\r\n");
- }
-
-
- virtual WDF_REQUEST_TYPE STDMETHODCALLTYPE GetType( void){
- + printf("[REQ] GetType\n"); fflush(stdout);
- printf("GetType\r\n");
- return reqType;
- }
- @@ -684,6 +734,10 @@ struct MyRequest : public IWDFIoRequest {
- _Out_opt_ USHORT *pFileAttributes,
- /* [annotation][unique][out] */
- _Out_opt_ USHORT *pShareAccess){
- + if(pOptions) *pOptions = 0;
- + if(pFileAttributes) *pFileAttributes = 0;
- + if(pShareAccess) *pShareAccess = 3;
- +
- printf("GetCreateParameters\r\n");
- }
-
- @@ -695,6 +749,10 @@ struct MyRequest : public IWDFIoRequest {
- _Out_opt_ LONGLONG *pullOffset,
- /* [annotation][unique][out] */
- _Out_opt_ ULONG *pulKey){
- + if(pSizeInBytes) *pSizeInBytes = 0;
- + if(pullOffset) *pullOffset = 0;
- + if(pulKey) *pulKey = 0;
- +
- printf("GetReadParameters\r\n");
- }
-
- @@ -706,6 +764,10 @@ struct MyRequest : public IWDFIoRequest {
- _Out_opt_ LONGLONG *pullOffset,
- /* [annotation][unique][out] */
- _Out_opt_ ULONG *pulKey){
- + if(pSizeInBytes) *pSizeInBytes = 0;
- + if(pullOffset) *pullOffset = 0;
- + if(pulKey) *pulKey = 0;
- +
- printf("GetWriteParameters\r\n");
- }
-
- @@ -717,6 +779,7 @@ struct MyRequest : public IWDFIoRequest {
- _Out_opt_ SIZE_T *pInBufferSize,
- /* [annotation][unique][out] */
- _Out_opt_ SIZE_T *pOutBufferSize){
- + printf("[REQ] GetDeviceIoControlParameters\n"); fflush(stdout);
- //printf("GetDeviceIoControlParameters %p %p %p\r\n", pControlCode, pInBufferSize, pOutBufferSize);
- *pControlCode = ctl;
- *pInBufferSize = inMem->size;
- @@ -729,6 +792,7 @@ struct MyRequest : public IWDFIoRequest {
- virtual void STDMETHODCALLTYPE GetOutputMemory(
- /* [annotation][out] */
- _Out_ IWDFMemory **ppWdfMemory){
- + printf("[REQ] GetOutputMemory\n"); fflush(stdout);
- *ppWdfMemory = outMem;
- //printf("GetOutputMemory\r\n");
- }
- @@ -736,6 +800,7 @@ struct MyRequest : public IWDFIoRequest {
- virtual void STDMETHODCALLTYPE GetInputMemory(
- /* [annotation][out] */
- _Out_ IWDFMemory **ppWdfMemory){
- + printf("[REQ] GetInputMemory\n"); fflush(stdout);
- *ppWdfMemory = inMem;
- // printf("GetInputMemory\r\n");
- }
- @@ -744,11 +809,13 @@ struct MyRequest : public IWDFIoRequest {
- virtual void STDMETHODCALLTYPE MarkCancelable(
- /* [annotation][in] */
- _In_ void *pCancelCallback){
- + printf("[REQ] MarkCancelable\n"); fflush(stdout);
- printf("MarkCancelable\r\n");
- //this->cancelCallback = pCancelCallback;
- }
-
- virtual HRESULT STDMETHODCALLTYPE UnmarkCancelable( void){
- + printf("[REQ] UnmarkCancelable\n"); fflush(stdout);
- printf("UnmarkCancelable\r\n");
- return 0;
- }
- @@ -763,6 +830,7 @@ struct MyRequest : public IWDFIoRequest {
- virtual HRESULT STDMETHODCALLTYPE ForwardToIoQueue(
- /* [annotation][in] */
- _In_ IWDFIoQueue *pDestination){
- + printf("[REQ] ForwardToIoQueue\n"); fflush(stdout);
- printf("ForwardToIoQueue\r\n");
- return 0;
- }
- @@ -775,6 +843,7 @@ struct MyRequest : public IWDFIoRequest {
- _In_ ULONG Flags,
- /* [annotation][in] */
- _In_ LONGLONG Timeout){
- + printf("[REQ] Send\n"); fflush(stdout);
- printf("Send\r\n");
- return 0;
- }
- @@ -783,6 +852,9 @@ struct MyRequest : public IWDFIoRequest {
- virtual void STDMETHODCALLTYPE GetFileObject(
- /* [annotation][out] */
- _Out_ void **ppFileObject){
- + printf("[REQ] GetFileObject\n"); fflush(stdout);
- + if(ppFileObject) *ppFileObject = NULL;
- +
- printf("GetFileObject\r\n");
- }
-
- @@ -801,6 +873,9 @@ struct MyRequest : public IWDFIoRequest {
- virtual void STDMETHODCALLTYPE GetIoQueue(
- /* [annotation][out] */
- _Out_ IWDFIoQueue **ppWdfIoQueue){
- + WUDF_TRACE("GetIoQueue -> %p\n", ppWdfIoQueue ? *ppWdfIoQueue : NULL);
- + if(ppWdfIoQueue) *ppWdfIoQueue = NULL;
- +
- printf("GetIoQueue\r\n");
- }
-
- @@ -826,12 +901,24 @@ struct MyRequest : public IWDFIoRequest {
- virtual void STDMETHODCALLTYPE GetCompletionParams(
- /* [annotation][out] */
- _Out_ IWDFRequestCompletionParams **ppCompletionParams){
- + printf("[REQ] GetCompletionParams\n"); fflush(stdout);
- + WUDF_TRACE("GetCompletionParams -> %p\n", ppCompletionParams ? *ppCompletionParams : NULL);
- + if(ppCompletionParams) *ppCompletionParams = NULL;
- +
- printf("GetCompletionParams\r\n");
- }
-
-
- };
-
- +static void *g_created_memory_buf = NULL;
- +static IWDFObject *g_wdf_object_stub = NULL;
- +static MyMem *g_dummy_file = NULL;
- +static MyMem *g_dummy_out_mem = NULL;
- +static MyMem *g_dummy_in_mem = NULL;
- +static IWDFIoRequest *g_request_stub = NULL;
- +static MyMem *g_file_stub = NULL;
- +
- struct MyPropertyStoreFactory : public IWDFPropertyStoreFactory {
- public:
- virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject) {
- @@ -875,6 +962,7 @@ public:
- };
-
- struct MyQueue : public IWDFIoQueue {
- + void *assigned_context = NULL;
- public:
- MyQueue(IUnknown *pCallbackInterface) {
- pCallbackInterface->AddRef();
- @@ -913,14 +1001,16 @@ struct MyQueue : public IWDFIoQueue {
- _In_opt_ __drv_aliasesMem void *pCleanupCallback,
- /* [annotation][unique][in] */
- _In_opt_ __drv_aliasesMem void *pContext) {
- - printf("AssignContext\r\n");
- + assigned_context = pContext;
- + printf("AssignContext(%p)\r\n", pContext);
- return 0;
- }
-
- virtual HRESULT STDMETHODCALLTYPE RetrieveContext(
- /* [annotation][out] */
- _Out_ void **ppvContext) {
- - printf("RetrieveContext\r\n");
- + if(ppvContext) *ppvContext = assigned_context;
- + printf("RetrieveContext -> %p\r\n", assigned_context);
- return 0;
- }
-
- @@ -935,6 +1025,9 @@ struct MyQueue : public IWDFIoQueue {
- virtual void STDMETHODCALLTYPE GetDevice(
- /* [annotation][out] */
- _Out_ IWDFDevice **ppWdfDevice){
- + WUDF_TRACE("GetDevice -> %p\n", ppWdfDevice ? *ppWdfDevice : NULL);
- + if(ppWdfDevice) *ppWdfDevice = NULL;
- +
- printf("GetDevice\r\n");
- }
-
- @@ -954,6 +1047,9 @@ struct MyQueue : public IWDFIoQueue {
- _Out_ ULONG *pulNumOfRequestsInQueue,
- /* [annotation][out] */
- _Out_ ULONG *pulNumOfRequestsInDriver){
- + if(pulNumOfRequestsInQueue) *pulNumOfRequestsInQueue = 0;
- + if(pulNumOfRequestsInDriver) *pulNumOfRequestsInDriver = 0;
- +
- printf("GetState\r\n");
- return (WDF_IO_QUEUE_STATE)(WdfIoQueueAcceptRequests |
- WdfIoQueueDispatchRequests |
- @@ -966,7 +1062,8 @@ struct MyQueue : public IWDFIoQueue {
- /* [annotation][out] */
- _Out_ IWDFIoRequest **ppRequest){
- printf("RetrieveNextRequest\r\n");
- - return 0;
- + if(ppRequest) *ppRequest = NULL;
- +return 0;
- }
-
-
- @@ -976,7 +1073,8 @@ struct MyQueue : public IWDFIoQueue {
- /* [annotation][out] */
- _Out_ IWDFIoRequest **ppRequest){
- printf("RetrieveNextRequestByFileObject\r\n");
- - return 0;
- + if(ppRequest) *ppRequest = NULL;
- +return 0;
- }
-
-
- @@ -1029,6 +1127,7 @@ MyQueue *myQueue = NULL;
-
-
- struct MyDevice : public IWDFDevice3 {
- + void *assigned_context = NULL;
- public:
- MyDevice(IUnknown *pCallbackInterface) {
- pCallbackInterface->AddRef();
- @@ -1075,14 +1174,16 @@ struct MyDevice : public IWDFDevice3 {
- _In_opt_ __drv_aliasesMem void *pCleanupCallback,
- /* [annotation][unique][in] */
- _In_opt_ __drv_aliasesMem void *pContext) {
- - printf("AssignContext\r\n");
- + assigned_context = pContext;
- + printf("AssignContext(%p)\r\n", pContext);
- return 0;
- }
-
- virtual HRESULT STDMETHODCALLTYPE RetrieveContext(
- /* [annotation][out] */
- _Out_ void **ppvContext) {
- - printf("RetrieveContext\r\n");
- + if(ppvContext) *ppvContext = assigned_context;
- + printf("RetrieveContext -> %p\r\n", assigned_context);
- return 0;
- }
-
- @@ -1113,6 +1214,9 @@ struct MyDevice : public IWDFDevice3 {
- virtual void STDMETHODCALLTYPE GetDriver(
- /* [annotation][out] */
- _Out_ IWDFDriver **ppWdfDriver){
- + WUDF_TRACE("GetDriver -> %p\n", ppWdfDriver ? *ppWdfDriver : NULL);
- + if(ppWdfDriver) *ppWdfDriver = NULL;
- +
- printf("GetDriver\r\n");
- }
-
- @@ -1130,6 +1234,9 @@ struct MyDevice : public IWDFDevice3 {
- virtual void STDMETHODCALLTYPE GetDefaultIoTarget(
- /* [annotation][out] */
- _Out_ IWDFIoTarget **ppWdfIoTarget){
- + WUDF_TRACE("GetDefaultIoTarget -> %p\n", ppWdfIoTarget ? *ppWdfIoTarget : NULL);
- + if(ppWdfIoTarget) *ppWdfIoTarget = NULL;
- +
- printf("GetDefaultIoTarget\r\n");
- }
-
- @@ -1140,6 +1247,10 @@ struct MyDevice : public IWDFDevice3 {
- /* [annotation][out] */
- _Out_ IWDFDriverCreatedFile **ppFile){
- printf("CreateWdfFile\r\n");
- + //Return a live object: the driver stores this as its file handle
- + //and dereferences it during I/O. NULL here crashes the capture path.
- + if(!g_dummy_file) g_dummy_file = new MyMem(malloc(16), 16);
- + if(ppFile) *ppFile = (IWDFDriverCreatedFile*) g_dummy_file;
- return 0;
- }
-
- @@ -1147,6 +1258,9 @@ struct MyDevice : public IWDFDevice3 {
- virtual void STDMETHODCALLTYPE GetDefaultIoQueue(
- /* [annotation][out] */
- _Out_ IWDFIoQueue **ppWdfIoQueue){
- + WUDF_TRACE("GetDefaultIoQueue -> %p\n", ppWdfIoQueue ? *ppWdfIoQueue : NULL);
- + if(ppWdfIoQueue) *ppWdfIoQueue = NULL;
- +
- printf("GetDefaultIoQueue\r\n");
- }
-
- @@ -1300,7 +1414,16 @@ struct MyDevice : public IWDFDevice3 {
- _In_opt_ IWDFObject *pParentObject,
- /* [annotation][out] */
- _Out_ IWDFIoRequest **ppRequest){
- + WUDF_TRACE("CreateRequest -> %p\n", ppRequest ? *ppRequest : NULL);
- printf("CreateRequest\r\n");
- + //Hand the driver a live request stub; a NULL here is dereferenced
- + //by the driver later in the capture path.
- + if(!g_request_stub) {
- + if(!g_dummy_out_mem) g_dummy_out_mem = new MyMem(malloc(256), 256);
- + if(!g_dummy_in_mem) g_dummy_in_mem = new MyMem(malloc(256), 256);
- + g_request_stub = (IWDFIoRequest*) new MyRequest(WdfRequestTypeOther, 0, g_dummy_out_mem, g_dummy_in_mem);
- + }
- + if(ppRequest) *ppRequest = g_request_stub;
- return 0;
- }
-
- @@ -1370,6 +1493,9 @@ struct MyDevice : public IWDFDevice3 {
- _In_opt_ IUnknown *pCallbackInterface,
- /* [annotation][out] */
- _Out_ void **ppRemoteInterface){
- + WUDF_TRACE("CreateRemoteInterface -> %p\n", ppRemoteInterface ? *ppRemoteInterface : NULL);
- + if(ppRemoteInterface) *ppRemoteInterface = NULL;
- +
- printf("CreateRemoteInterface\r\n");
- return 0;
- }
- @@ -1382,6 +1508,9 @@ struct MyDevice : public IWDFDevice3 {
- _In_opt_ IWDFObject *pParentObject,
- /* [annotation][out] */
- _Out_ void **ppRemoteTarget){
- + WUDF_TRACE("CreateRemoteTarget -> %p\n", ppRemoteTarget ? *ppRemoteTarget : NULL);
- + if(ppRemoteTarget) *ppRemoteTarget = NULL;
- +
- printf("CreateRemoteTarget\r\n");
- return 0;
- }
- @@ -1392,6 +1521,9 @@ struct MyDevice : public IWDFDevice3 {
- _Out_ WDF_DEVICE_IO_TYPE *ReadWritePreference,
- /* [annotation][out] */
- _Out_ WDF_DEVICE_IO_TYPE *IoControlPreference){
- + if(ReadWritePreference) *ReadWritePreference = (WDF_DEVICE_IO_TYPE) 0;
- + if(IoControlPreference) *IoControlPreference = (WDF_DEVICE_IO_TYPE) 0;
- +
- printf("GetDeviceStackIoTypePreference\r\n");
- }
-
- @@ -1424,6 +1556,8 @@ struct MyDevice : public IWDFDevice3 {
- _In_ MEMORY_CACHING_TYPE CacheType,
- /* [annotation][out] */
- _Out_ void **pPseudoBaseAddress){
- + if(pPseudoBaseAddress) *pPseudoBaseAddress = NULL;
- +
- printf("MapIoSpace\r\n");
- return 0;
- }
- @@ -1484,6 +1618,8 @@ struct MyDevice : public IWDFDevice3 {
- _In_ void* Configuration,
- /* [annotation][out] */
- _Out_ void **ppInterrupt){
- + if(ppInterrupt) *ppInterrupt = NULL;
- +
- printf("CreateInterrupt\r\n");
- return 0;
- }
- @@ -1496,6 +1632,8 @@ struct MyDevice : public IWDFDevice3 {
- _In_ IWDFObject *pParentObject,
- /* [annotation][out] */
- _Out_ void **ppWorkItem){
- + if(ppWorkItem) *ppWorkItem = NULL;
- +
- printf("CreateWorkItem\r\n");
- return 0;
- }
- @@ -1514,10 +1652,14 @@ struct MyDevice : public IWDFDevice3 {
- MyDevice* myDevice = NULL;
-
-
- +
- +
- struct MyDriver : public IWDFDriver {
- + void *assigned_context = NULL;
- public:
- virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject) {
- - printf("QueryInterface\r\n");
- + LPOLESTR _s; StringFromIID(riid, &_s);
- + std::wcout << L"[QI] " << _s << (ppvObject ? (void*)*ppvObject : (void*)0) << std::endl;
- return 0;
- }
- virtual ULONG STDMETHODCALLTYPE AddRef(void) {
- @@ -1540,14 +1682,16 @@ struct MyDriver : public IWDFDriver {
- _In_opt_ __drv_aliasesMem void *pCleanupCallback,
- /* [annotation][unique][in] */
- _In_opt_ __drv_aliasesMem void *pContext) {
- - printf("AssignContext\r\n");
- + assigned_context = pContext;
- + printf("AssignContext(%p)\r\n", pContext);
- return 0;
- }
-
- virtual HRESULT STDMETHODCALLTYPE RetrieveContext(
- /* [annotation][out] */
- _Out_ void **ppvContext) {
- - printf("RetrieveContext\r\n");
- + if(ppvContext) *ppvContext = assigned_context;
- + printf("RetrieveContext -> %p\r\n", assigned_context);
- return 0;
- }
-
- @@ -1585,6 +1729,12 @@ struct MyDriver : public IWDFDriver {
- /* [annotation][out] */
- _Out_ IWDFObject **ppWdfObject) {
- printf("CreateWdfObject\r\n");
- + //Return a minimal generic object. The driver only stores and later
- + //releases/context-manages these; a live stub keeps that safe.
- + if(ppWdfObject) {
- + if(!g_wdf_object_stub) g_wdf_object_stub = (IWDFObject*) new MyMem(malloc(16), 16);
- + *ppWdfObject = g_wdf_object_stub;
- + }
- return 0;
- }
-
- @@ -1599,7 +1749,8 @@ struct MyDriver : public IWDFDriver {
- _In_opt_ IWDFObject *pParentObject,
- /* [annotation][out] */
- _Out_ IWDFMemory **ppWdfMemory) {
- - printf("CreatePreallocatedWdfMemory\r\n");
- + printf("CreatePreallocatedWdfMemory(%p, %zu)\r\n", pBuff, BufferSize);
- + if(ppWdfMemory) *ppWdfMemory = new MyMem(pBuff, BufferSize);
- return 0;
- }
-
- @@ -1612,7 +1763,9 @@ struct MyDriver : public IWDFDriver {
- _In_opt_ IWDFObject *pParentObject,
- /* [annotation][out] */
- _Out_ IWDFMemory **ppWdfMemory) {
- - printf("CreateWdfMemory\r\n");
- + printf("CreateWdfMemory(%zu)\r\n", BufferSize);
- + g_created_memory_buf = malloc(BufferSize);
- + if(ppWdfMemory) *ppWdfMemory = new MyMem(g_created_memory_buf, BufferSize);
- return 0;
- }
-
- @@ -1650,25 +1803,49 @@ static NTSTATUS tudor_devctrl_wudf1(struct tudor_device *device, OVERLAPPED *ovl
- printf("[WUDF1-DEVCTRL] -> code 0x%x (size 0x%lx)\r\n", code, in_size);
- }
-
- - // For UMDF1, we need to handle this through the queue callback mechanism
- - // The actual I/O will be handled when the driver calls our queue callbacks
- -
- - // Store the request info for when the callback is invoked
- - // This is a simplified approach - you might need a more sophisticated request tracking system
- -
- if (myQueue) {
- - //MyMem in(ibuf, sizeof(ibuf)), out(obuf, sizeof(obuf));
- - MyMem in(in_buf, in_size), out(out_buf, out_size);
- - MyRequest req(WdfRequestTypeOther, 0x442004, &out, &in);
- -
- - printf("about to ioctl: 0x%x\r\n", code);
- - myQueue->ioctl->OnDeviceIoControl(myQueue, &req, code, 0, 0);
- - while(!req.complete)
- - usleep(20000);
- + //HEAP-allocate the memory/request objects: the driver retains pointers
- + //to them past this call (async completion, pending I/O). Stack objects
- + //were invalidated when this frame returned, leaving the driver with
- + //dangling pointers - the root cause of the capture-path null crash.
- + MyMem *in = new MyMem(in_buf, in_size);
- + MyMem *out = new MyMem(out_buf, out_size);
- + MyRequest *req = new MyRequest(WdfRequestTypeOther, code, out, in);
- +
- + printf("about to ioctl: 0x%x (queue=%p cb=%p)\r\n", code, (void*) myQueue, (void*) myQueue->ioctl);
- + fflush(stdout);
- + myQueue->ioctl->OnDeviceIoControl(myQueue, req, code, 0, 0);
- + printf("OnDeviceIoControl returned for 0x%x\r\n", code);
- + fflush(stdout);
- + while(!req->complete)
- + usleep(5000);
- + printf("ioctl 0x%x completed: status=0x%lx info=%lld\r\n",
- + code, (unsigned long) req->completionStatus, (long long) req->informationSize);
- + fflush(stdout);
- +
- + //The caller (DeviceIoControl) hands us an OVERLAPPED it will later
- + //wait on via GetOverlappedResult. Complete it with the driver's real
- + //status, otherwise the wait never ends.
- + size_t num_transfered = (size_t) req->informationSize;
- + if(num_transfered > out_size) num_transfered = out_size;
- +
- + HRESULT completion = req->completionStatus;
- + if(completion == 0) {
- + winio_complete_overlapped(ovlp, STATUS_SUCCESS, num_transfered);
- + return STATUS_SUCCESS;
- + }
- + if((completion & 0xffff0000) == 0x80070000) {
- + NTSTATUS status = (NTSTATUS) (completion & 0xffff);
- + winio_complete_overlapped(ovlp, status, 0);
- + return status;
- + }
- + winio_complete_overlapped(ovlp, (NTSTATUS) 0xC0000001, 0);
- + return (NTSTATUS) 0xC0000001; //STATUS_UNSUCCESSFUL
- }
- -
- - // For now, return pending - the actual completion will happen in the callback
- - return STATUS_PENDING;
- +
- + //No queue: treat as completed successfully
- + winio_complete_overlapped(ovlp, STATUS_SUCCESS, 0);
- + return STATUS_SUCCESS;
- }
-
- static NTSTATUS tudor_cancel_wudf1(struct tudor_device *device, OVERLAPPED *ovlp, struct winwdf_request *req) {
- @@ -1796,7 +1973,9 @@ bool tudor_init() {
- print_vtable(myDevice->pnphwcb, 5);
-
- rc = myDevice->pnphwcb->OnPrepareHardware(myDevice);
- - usleep(50000000);
- + //Settle time for the device's pairing/firmware handshake. The device
- + //re-enumerates during this window; 2s has proven sufficient.
- + usleep(2000000);
- printf("OnPrepareHardware rc = %lx\r\n", rc);
- fflush(stdout);
- //
- @@ -1804,11 +1983,11 @@ bool tudor_init() {
- abort();
- }
- //
- - usleep(1000000);
- + usleep(200000);
- printf("about to enter D0 state\r\n");
- rc = myDevice->pnpcb->OnD0Entry(myDevice, WdfPowerDeviceInvalid);
- printf("OnD0Entry rc = %lx\r\n", rc);
- - usleep(5000000);
- + usleep(300000);
- fflush(stdout);
-
-
- @@ -1816,15 +1995,88 @@ bool tudor_init() {
- winmodule_set_cur(&tudor_adapter_dll->module);
-
- HRESULT hres;
- - if((hres = ((api_WbioQuerySensorInterface) find_dll_export(&tudor_adapter_dll->image, "WbioQuerySensorInterface"))(&tudor_sensor_adapter)) != 0) {
- + //EXPERIMENT: skip the engine interface entirely. Its query/calls write
- + //runtime state that overlaps the sensor interface's static storage in
- + //the relinked adapter image, corrupting the sensor table.
- + if(getenv("TUDOR_NO_ENGINE") == NULL) {
- + if((hres = ((api_WbioQueryEngineInterface) find_dll_export(&tudor_adapter_dll->image, "WbioQueryEngineInterface"))(&tudor_engine_adapter)) != 0) {
- + log_error("Error querying engine interface: 0x%x!", hres);
- + return false;
- + }
- + } else {
- + printf("!! TUDOR_NO_ENGINE: engine interface disabled\n");
- + tudor_engine_adapter = (WINBIO_ENGINE_INTERFACE*) calloc(1, sizeof(WINBIO_ENGINE_INTERFACE));
- + //Dummy implementations: succeed and don't touch the pipeline
- + tudor_engine_adapter->Attach = (IBIO_ENGINE_ATTACH_FN*) dummy_engine_attach;
- + tudor_engine_adapter->PipelineInit = (IBIO_ENGINE_PIPELINE_INIT_FN*) dummy_engine_pipeline_init;
- + tudor_engine_adapter->PipelineCleanup = (IBIO_ENGINE_PIPELINE_CLEANUP_FN*) dummy_engine_pipeline_cleanup;
- + tudor_engine_adapter->Activate = (IBIO_ENGINE_ACTIVATE_FN*) dummy_engine_activate;
- + tudor_engine_adapter->Deactivate = (IBIO_ENGINE_DEACTIVATE_FN*) dummy_engine_deactivate;
- + }
- + if((hres = ((api_WbioQuerySensorInterface) find_dll_export(&tudor_adapter_dll->image, "WbioQuerySensorInterface"))((WINBIO_SENSOR_INTERFACE**) &tudor_sensor_adapter)) != 0) {
- log_error("Error querying sensor interface: 0x%x!", hres);
- return false;
- }
- - if((hres = ((api_WbioQueryEngineInterface) find_dll_export(&tudor_adapter_dll->image, "WbioQueryEngineInterface"))(&tudor_engine_adapter)) != 0) {
- - log_error("Error querying engine interface: 0x%x!", hres);
- - return false;
- +
- + //Snapshot the sensor interface table, then restore every function slot
- + //from the pristine static image. The relinked adapter's runtime state
- + //overlaps the table's storage in .data and clobbers slots; the file RVAs
- + //are stable, so normalize all 15 slots in one step.
- + {
- + WINBIO_SENSOR_INTERFACE_V1 *snapshot = (WINBIO_SENSOR_INTERFACE_V1*) malloc(sizeof(WINBIO_SENSOR_INTERFACE_V1));
- + memcpy(snapshot, tudor_sensor_adapter, sizeof(WINBIO_SENSOR_INTERFACE_V1));
- + tudor_sensor_adapter = snapshot;
- +
- + uintptr_t mod_base = (uintptr_t) tudor_adapter_dll->image.base_addr;
- + static const uint32_t sensor_slot_rvas[15] = {
- + 0x005b50, 0x005d50, 0x005ee0, 0x005ff0, 0x006250,
- + 0x006460, 0x0065d0, 0x006660, 0x0066f0, 0x006bf0,
- + 0x006ec0, 0x006f50, 0x0070b0, 0x0072f0, 0x0075b0,
- + };
- + void **slots = (void**) tudor_sensor_adapter;
- + for(int i = 0; i < 15; i++) {
- + slots[4 + i] = (void*) (mod_base + sensor_slot_rvas[i]);
- + }
- +
- + //Alias the Windows 10 names onto the legacy entry points. This driver
- + //predates the pipeline API; Reset doubles as init/activate and Cancel
- + //as cleanup.
- + tudor_sensor_adapter->PipelineInit = (IBIO_SENSOR_PIPELINE_INIT_FN*) tudor_sensor_adapter->Reset;
- + tudor_sensor_adapter->PipelineCleanup = (IBIO_SENSOR_PIPELINE_CLEANUP_FN*) tudor_sensor_adapter->Cancel;
- + tudor_sensor_adapter->Activate = (IBIO_SENSOR_ACTIVATE_FN*) tudor_sensor_adapter->Reset;
- + tudor_sensor_adapter->Deactivate = (IBIO_SENSOR_DEACTIVATE_FN*) tudor_sensor_adapter->Cancel;
- }
-
- + //Debug: dump the queried interface tables so we can spot layout mismatches
- + printf("sensor adapter: version=%u type=%u size=%zu ptr=%p\n",
- + tudor_sensor_adapter->Version, tudor_sensor_adapter->Type, (size_t) tudor_sensor_adapter->Size, (void*) tudor_sensor_adapter);
- + printf(" Attach=%p Detach=%p ClearContext=%p QueryStatus=%p Reset=%p\n",
- + (void*) tudor_sensor_adapter->Attach, (void*) tudor_sensor_adapter->Detach,
- + (void*) tudor_sensor_adapter->ClearContext, (void*) tudor_sensor_adapter->QueryStatus,
- + (void*) tudor_sensor_adapter->Reset);
- + printf(" PipelineInit=%p PipelineCleanup=%p Activate=%p Deactivate=%p\n",
- + (void*) tudor_sensor_adapter->PipelineInit, (void*) tudor_sensor_adapter->PipelineCleanup,
- + (void*) tudor_sensor_adapter->Activate, (void*) tudor_sensor_adapter->Deactivate);
- + printf("engine adapter: version=%u type=%u size=%zu ptr=%p\n",
- + tudor_engine_adapter->Version, tudor_engine_adapter->Type, (size_t) tudor_engine_adapter->Size, (void*) tudor_engine_adapter);
- + //Even if the struct layout differs, print raw words at the expected offsets
- + printf("RAW sensor table:");
- + for(int i = 0; i < 12; i++) printf(" %016zx", ((size_t*) tudor_sensor_adapter)[i + 4]);
- + printf("\n");
- + printf("ADDRSensor=%p ADDREngine=%p\n", (void*) tudor_sensor_adapter, (void*) tudor_engine_adapter);
- + printf("ENGINE table:");
- + for(int i = 0; i < 16; i++) printf(" %016zx", ((size_t*) tudor_engine_adapter)[i + 4]);
- + printf("\n");
- + printf("ENGINE+0x60=%p ENGINE+0x100=%p\n",
- + (void*) *(void**) ((char*) tudor_engine_adapter + 0x60),
- + (void*) *(void**) ((char*) tudor_engine_adapter + 0x100));
- + printf(" Attach=%p Detach=%p ClearContext=%p\n",
- + (void*) tudor_engine_adapter->Attach, (void*) tudor_engine_adapter->Detach,
- + (void*) tudor_engine_adapter->ClearContext);
- + printf(" PipelineInit=%p PipelineCleanup=%p Activate=%p Deactivate=%p\n",
- + (void*) tudor_engine_adapter->PipelineInit, (void*) tudor_engine_adapter->PipelineCleanup,
- + (void*) tudor_engine_adapter->Activate, (void*) tudor_engine_adapter->Deactivate);
- +
- printf("tudor_init finish!\n");
- return true;
-
- @@ -1917,8 +2169,8 @@ bool tudor_open(struct tudor_device *device, libusb_device_handle *usb_dev, stru
- if(!device->pipeline) { perror("Error allocating WINBIO pipeline"); abort(); }
- *device->pipeline = (WINBIO_PIPELINE) {0};
- device->pipeline->EngineInterface = tudor_engine_adapter;
- - device->pipeline->SensorInterface = tudor_sensor_adapter;
- - // device->pipeline->StorageInterface = tudor_storage_adapter;
- + device->pipeline->SensorInterface = (WINBIO_SENSOR_INTERFACE*) tudor_sensor_adapter;
- + device->pipeline->StorageInterface = tudor_storage_adapter;
- // device->pipeline->SensorHandle = device->winbio_file = winio_create_file(device, true, NULL, NULL, (winio_devctrl_fnc*) tudor_devctrl, (winio_cancel_fnc*) tudor_cancel, (winio_cleanup_fnc*) tudor_cleanup, NULL);
- device->pipeline->SensorHandle = device->winbio_file = winio_create_file(
- device,
- @@ -1936,32 +2188,60 @@ bool tudor_open(struct tudor_device *device, libusb_device_handle *usb_dev, stru
- device->pipeline->StorageContext = device;
-
- log_debug("Attaching interfaces to pipeline...");
- - WINBIO_CALL_PIPELINE(tudor_sensor_adapter->Attach, device->pipeline);
- - WINBIO_CALL_PIPELINE(tudor_engine_adapter->Attach, device->pipeline);
- - // WINBIO_CALL_PIPELINE(tudor_storage_adapter->Attach, device->pipeline);
- + WINBIO_CALL_PIPELINE_RC("sensor.Attach", tudor_sensor_adapter->Attach, device->pipeline);
- + if(getenv("TUDOR_NO_ENGINE") == NULL) {
- + WINBIO_CALL_PIPELINE_RC("engine.Attach", tudor_engine_adapter->Attach, device->pipeline);
- + }
- + WINBIO_CALL_PIPELINE_RC("storage.Attach", tudor_storage_adapter->Attach, device->pipeline);
- +
- + printf("CTX: sensor=%p engine=%p storage=%p\n",
- + device->pipeline->SensorContext, device->pipeline->EngineContext, device->pipeline->StorageContext);
-
- log_debug("Initializing pipeline interfaces...");
- - WINBIO_CALL_PIPELINE(tudor_sensor_adapter->PipelineInit, device->pipeline);
- - WINBIO_CALL_PIPELINE(tudor_engine_adapter->PipelineInit, device->pipeline);
- - // WINBIO_CALL_PIPELINE(tudor_storage_adapter->PipelineInit, device->pipeline);
- + //NOTE: this driver (Synaptics WBDI 5.5.2731.1050) predates the Windows 10
- + //sensor pipeline API: its interface table has no PipelineInit or Activate
- + //entries (see WINBIO_SENSOR_INTERFACE_V1). Attach plus Reset is the whole
- + //initialization sequence for it.
- + if(getenv("TUDOR_PROBE_SLOT")) {
- + int slot = atoi(getenv("TUDOR_PROBE_SLOT"));
- + void **tbl = (void**) tudor_sensor_adapter;
- + fflush(stdout);
- + printf("PROBE SLOT %d (+%#04x): %p ...\n", slot, 0x20 + slot*8, tbl[4 + slot]);
- + fflush(stdout);
- + HRESULT r = ((HRESULT (*)(WINBIO_PIPELINE*)) tbl[4 + slot])(device->pipeline);
- + printf("PROBE SLOT %d -> 0x%x\n", slot, r);
- + fflush(stdout);
- + }
-
- //Reset the sensor
- log_debug("Resetting sensor...");
- - WINBIO_CALL_PIPELINE(tudor_sensor_adapter->Reset, device->pipeline);
- -
- - //Activate the pipeline
- - log_debug("Activating pipeline...");
- - WINBIO_CALL_PIPELINE(tudor_sensor_adapter->Activate, device->pipeline);
- - WINBIO_CALL_PIPELINE(tudor_engine_adapter->Activate, device->pipeline);
- - // WINBIO_CALL_PIPELINE(tudor_storage_adapter->Activate, device->pipeline);
- + //The driver's reset may need time to settle after firmware init
- + //(returns ERROR_NO_MORE_ITEMS while busy). Retry with backoff.
- + {
- + hres = 0x80070103 /* ERROR_NO_MORE_ITEMS */;
- + for(int attempt = 0; attempt < 20; attempt++) {
- + hres = tudor_sensor_adapter->Reset(device->pipeline);
- + printf("[PIPE] sensor.Reset attempt %d -> 0x%x\n", attempt, hres);
- + if(hres == ERROR_SUCCESS) break;
- + usleep(500000);
- + }
- + if(hres != ERROR_SUCCESS) { log_error("Reset did not succeed: 0x%x", hres); return false; }
- + }
-
- //Check the sensor status
- log_debug("Checking sensor status...");
- ULONG sensor_status = WINBIO_SENSOR_FAILURE;
- - WINBIO_CALL_PIPELINE(tudor_sensor_adapter->QueryStatus, device->pipeline, &sensor_status)
- + WINBIO_CALL_PIPELINE_RC("sensor.QueryStatus", tudor_sensor_adapter->QueryStatus, device->pipeline, &sensor_status);
- + printf("[PIPE] sensor status = 0x%x\n", sensor_status);
- if(sensor_status != WINBIO_SENSOR_READY) {
- - log_error("Sensor didn't return ready status! [status 0x%x]", status);
- - return false;
- + //NOT_CALIBRATED (5) is expected until the sensor performs its first
- + //capture cycle; only a hard failure should block.
- + if(sensor_status == WINBIO_SENSOR_NOT_CALIBRATED) {
- + log_warn("Sensor not calibrated yet, continuing [status 0x%x]", sensor_status);
- + } else {
- + log_error("Sensor didn't return ready status! [status 0x%x]", sensor_status);
- + return false;
- + }
- }
-
- return true;
- diff --git a/libtudor/src/tudor/internal.h b/libtudor/src/tudor/internal.h
- index c3f0ac9..e5d58e3 100644
- --- a/libtudor/src/tudor/internal.h
- +++ b/libtudor/src/tudor/internal.h
- @@ -58,10 +58,15 @@ struct windrv_dll {
- extern struct windrv_dll tudor_windrv_dlls[];
-
- #define WINBIO_CALL_PIPELINE(fnc, ...) if((hres = fnc(__VA_ARGS__)) != ERROR_SUCCESS) { log_error("Error in WINBIO pipeline function '%s': 0x%x!", #fnc, hres); return false; }
- +#define WINBIO_CALL_PIPELINE_RC(name, fnc, ...) do { \
- + hres = (fnc)(__VA_ARGS__); \
- + printf("[PIPE] %s -> 0x%x\n", name, hres); \
- + if(hres != ERROR_SUCCESS) { log_error("Error in WINBIO pipeline function '%s': 0x%x!", name, hres); return false; } \
- +} while(0)
-
- extern struct windrv_dll *tudor_adapter_dll, *tudor_driver_dll;
- extern struct winwdf_driver *tudor_wdf_driver;
- -extern WINBIO_SENSOR_INTERFACE *tudor_sensor_adapter;
- +extern WINBIO_SENSOR_INTERFACE_V1 *tudor_sensor_adapter;
- extern WINBIO_ENGINE_INTERFACE *tudor_engine_adapter;
- extern WINBIO_STORAGE_INTERFACE *tudor_storage_adapter;
-
- diff --git a/libtudor/src/tudor/winbio.h b/libtudor/src/tudor/winbio.h
- index 0086f3c..06f5c74 100644
- --- a/libtudor/src/tudor/winbio.h
- +++ b/libtudor/src/tudor/winbio.h
- @@ -428,6 +428,44 @@ typedef struct _WINBIO_SENSOR_INTERFACE {
- IBIO_SENSOR_QUERY_PRIVATE_SENSOR_TYPE_FN *QueryPrivateSensorType;
- } WINBIO_SENSOR_INTERFACE;
-
- +//Legacy sensor interface layout as used by the Synaptics WBDI sensor adapter
- +//5.5.2731.1050. Empirically verified against the adapter binary (function
- +//trace strings + probe calls): this is a pre-Windows-10 adapter, so it has
- +//no native PipelineInit/PipelineCleanup/Activate/Deactivate entries. The
- +//older code paths spell those operations differently, so the modern names
- +//are aliased onto the closest legacy function (Reset for "init/activate",
- +//Cancel for "cleanup"), which is what the Windows 7 pipeline did.
- +typedef struct _WINBIO_SENSOR_INTERFACE_V1 {
- + WINBIO_ADAPTER_INTERFACE_VERSION Version;
- + ULONG Type;
- + SIZE_T Size;
- + GUID AdapterId;
- + IBIO_SENSOR_ATTACH_FN *Attach;
- + IBIO_SENSOR_DETACH_FN *Detach;
- + IBIO_SENSOR_CLEAR_CONTEXT_FN *ClearContext;
- + IBIO_SENSOR_QUERY_STATUS_FN *QueryStatus;
- + IBIO_SENSOR_RESET_FN *Reset;
- + IBIO_SENSOR_SET_MODE_FN *SetMode;
- + IBIO_SENSOR_SET_INDICATOR_STATUS_FN *SetIndicatorStatus;
- + IBIO_SENSOR_GET_INDICATOR_STATUS_FN *GetIndicatorStatus;
- + IBIO_SENSOR_START_CAPTURE_FN *StartCapture;
- + IBIO_SENSOR_FINISH_CAPTURE_FN *FinishCapture;
- + IBIO_SENSOR_EXPORT_SENSOR_DATA_FN *ExportSensorData;
- + IBIO_SENSOR_CANCEL_FN *Cancel;
- + IBIO_SENSOR_PUSH_DATA_TO_ENGINE_FN *PushDataToEngine;
- + IBIO_SENSOR_CONTROL_UNIT_FN *ControlUnit;
- + IBIO_SENSOR_CONTROL_UNIT_PRIVILEGED_FN *ResetOwnership;
- +
- + //Aliases for legacy call sites that use the Windows 10 names. These are
- + //not separate table slots: PipelineInit/PipelineCleanup/Activate/
- + //Deactivate map onto the legacy Reset/Cancel entry points, which is the
- + //closest equivalent behaviour for this driver generation.
- + IBIO_SENSOR_PIPELINE_INIT_FN *PipelineInit;
- + IBIO_SENSOR_PIPELINE_CLEANUP_FN *PipelineCleanup;
- + IBIO_SENSOR_ACTIVATE_FN *Activate;
- + IBIO_SENSOR_DEACTIVATE_FN *Deactivate;
- +} WINBIO_SENSOR_INTERFACE_V1;
- +
- typedef HRESULT __winfnc IBIO_ENGINE_ATTACH_FN(WINBIO_PIPELINE *Pipeline);
- typedef HRESULT __winfnc IBIO_ENGINE_DETACH_FN(WINBIO_PIPELINE *Pipeline);
- typedef HRESULT __winfnc IBIO_ENGINE_CLEAR_CONTEXT_FN(WINBIO_PIPELINE *Pipeline);
- diff --git a/libtudor/src/winapi/error.c b/libtudor/src/winapi/error.c
- index a4318c1..1dcd77e 100644
- --- a/libtudor/src/winapi/error.c
- +++ b/libtudor/src/winapi/error.c
- @@ -3,6 +3,10 @@
- #include "internal.h"
-
- static __thread DWORD last_error;
- +static __thread int get_last_error_trace_count;
- +
- +//Set by the CLI when tracing is wanted; limits noise
- +bool tudor_trace_errors = false;
-
- void winerr_clear() {
- WIN_CLOBBER_NONVOL_REGS
- @@ -31,7 +35,10 @@ int winerr_from_errno() {
-
- __winfnc DWORD GetLastError() {
- WIN_CLOBBER_NONVOL_REGS
- - // printf("Get last_error = %d\n", last_error);
- + if(tudor_trace_errors && get_last_error_trace_count < 100) {
- + printf("[DBG] GetLastError -> 0x%x\n", last_error);
- + get_last_error_trace_count++;
- + }
- return last_error;
- }
- WINAPI(GetLastError)
- diff --git a/libtudor/src/winapi/io.c b/libtudor/src/winapi/io.c
- index e58fcfa..7050a05 100644
- --- a/libtudor/src/winapi/io.c
- +++ b/libtudor/src/winapi/io.c
- @@ -266,11 +266,19 @@ __winfnc BOOL DeviceIoControl(HANDLE handle, DWORD code, const void *in_buf, DWO
- //Start the operation
- NTSTATUS status = file->devctrl_fnc(file->ctx, ovlp, code, in_buf, in_size, out_buf, out_size, op_ctx);
- winerr_set_code(status);
- - if(status != STATUS_SUCCESS) return FALSE;
- + if(status != STATUS_SUCCESS && status != STATUS_PENDING) return FALSE;
-
- if(file->is_async) {
- - winerr_set_code(ERROR_IO_PENDING);
- - return FALSE;
- + //Windows semantics: an overlapped request tells the caller to wait
- + //via GetOverlappedResult with ERROR_IO_PENDING (0x3e5), not the raw
- + //NTSTATUS value. The overlapped must be completed by the backend.
- + if(ovlp->Internal == STATUS_PENDING) {
- + winerr_set_code(ERROR_IO_PENDING);
- + return FALSE;
- + }
- + *out_ret = (DWORD) ovlp->InternalHigh;
- + winerr_clear();
- + return TRUE;
- }
-
- //Wait for the operation to complete
- diff --git a/libtudor/src/winapi/pipe.c b/libtudor/src/winapi/pipe.c
- index 1a582e0..0ab6da5 100644
- --- a/libtudor/src/winapi/pipe.c
- +++ b/libtudor/src/winapi/pipe.c
- @@ -108,7 +108,8 @@ __winfnc HRESULT SHGetFolderPathA(
- TRACE();
- int folder = CSIDL_FOLDER_MASK & nFolder;
- printf("SHGetFolderPathA: %d\n", folder);
- - const char* path = "/home/rstar000/synaptics";
- + const char *folder_env = getenv("TUDOR_DATA_DIR");
- + const char* path = (folder_env && folder_env[0]) ? folder_env : "/tmp/tudor-synaptics";
- if (pszPath == NULL) {
- pszPath = malloc(sizeof(path + 1));
- }
- diff --git a/libtudor/src/winapi/winusb.c b/libtudor/src/winapi/winusb.c
- index bc26227..44189f8 100644
- --- a/libtudor/src/winapi/winusb.c
- +++ b/libtudor/src/winapi/winusb.c
- @@ -45,6 +45,14 @@ struct driver_info {
- struct libusb_device_descriptor descr;
- libusb_device_handle * dev;
-
- + // Device location on the USB bus tree, used to re-acquire the sensor
- + // after it re-enumerates (the driver forces device reconnects during
- + // its trust/calibration cycles).
- + uint8_t bus_number;
- + uint8_t port_numbers[8];
- + int port_numbers_len;
- + pthread_mutex_t reopen_lock;
- +
- DWORD timeouts[0x100];
- volatile char abort[0x100];
-
- @@ -83,7 +91,12 @@ bool claim_device(struct driver_info *info)
- err(libusb_set_configuration(info->dev, 1));
- err(libusb_claim_interface(info->dev, 0));
-
- - printf("libusb initialized, device = %p\n", info->dev);
- + //Remember the device's position on the bus tree
- + info->bus_number = libusb_get_bus_number(dev_list[i]);
- + info->port_numbers_len = libusb_get_port_numbers(dev_list[i], info->port_numbers, sizeof(info->port_numbers));
- + if(info->port_numbers_len < 0) info->port_numbers_len = 0;
- +
- + printf("libusb initialized, device = %p [bus %d, port path %d]\n", info->dev, info->bus_number, info->port_numbers_len ? info->port_numbers[0] : -1);
- return TRUE;
- }
- }
- @@ -93,6 +106,95 @@ bool claim_device(struct driver_info *info)
- return FALSE;
- }
-
- +static bool device_matches_location(struct driver_info *info, libusb_device *dev)
- +{
- + if(libusb_get_bus_number(dev) != info->bus_number) return false;
- +
- + uint8_t ports[8];
- + int len = libusb_get_port_numbers(dev, ports, sizeof(ports));
- + if(len != info->port_numbers_len) return false;
- +
- + return memcmp(ports, info->port_numbers, len) == 0;
- +}
- +
- +// Re-acquires the sensor after it has re-enumerated on the bus. The driver
- +// intentionally forces the device to disconnect/reconnect during its
- +// initialization cycles, which invalidates the old handle. Wait for the
- +// device to come back at the same bus location and open it again.
- +static bool reopen_device_once(struct driver_info *info)
- +{
- + int rc;
- + pthread_mutex_lock(&info->reopen_lock);
- +
- + fprintf(stderr, "** device disconnected, waiting for it to reappear...\n");
- +
- + //The old handle is dead once the device has re-enumerated
- + if(info->dev) {
- + libusb_close(info->dev);
- + info->dev = NULL;
- + }
- +
- + //Poll for the device to come back (up to ~20 seconds)
- + for(int attempt = 0; attempt < 100; attempt++) {
- + libusb_device ** dev_list;
- + int dev_cnt = libusb_get_device_list(info->ctx, &dev_list);
- + if(dev_cnt < 0) {
- + pthread_mutex_unlock(&info->reopen_lock);
- + return false;
- + }
- +
- + for(int i = 0; i < dev_cnt; i++) {
- + libusb_device *dev = dev_list[i];
- +
- + if(!device_matches_location(info, dev)) continue;
- +
- + struct libusb_device_descriptor descriptor;
- + if(libusb_get_device_descriptor(dev, &descriptor) != 0) continue;
- + if(descriptor.idVendor != info->supported_devices[0].vend_id ||
- + descriptor.idProduct != info->supported_devices[0].prod_id) continue;
- +
- + rc = libusb_open(dev, &info->dev);
- + if(rc == 0) {
- + rc = libusb_set_configuration(info->dev, 1);
- + if(rc != 0 && rc != LIBUSB_ERROR_BUSY) {
- + libusb_close(info->dev);
- + info->dev = NULL;
- + continue;
- + }
- + rc = libusb_claim_interface(info->dev, 0);
- + if(rc != 0) {
- + libusb_close(info->dev);
- + info->dev = NULL;
- + continue;
- + }
- +
- + libusb_free_device_list(dev_list, 1);
- + fprintf(stderr, "** device re-acquired [bus %d]\n", info->bus_number);
- + pthread_mutex_unlock(&info->reopen_lock);
- + return true;
- + }
- + }
- +
- + libusb_free_device_list(dev_list, 1);
- +
- + struct timeval tv = { .tv_sec = 0, .tv_usec = 200000 };
- + libusb_handle_events_timeout(info->ctx, &tv);
- + usleep(200000);
- + }
- +
- + fprintf(stderr, "** device did not come back\n");
- + pthread_mutex_unlock(&info->reopen_lock);
- + return false;
- +}
- +
- +//Handle a transfer error: if the device has vanished, re-acquire it and
- +//report whether the caller should retry the transfer.
- +static bool handle_usb_error(struct driver_info *info, int rc)
- +{
- + if(rc != LIBUSB_ERROR_NO_DEVICE && rc != LIBUSB_ERROR_IO) return false;
- + return reopen_device_once(info);
- +}
- +
-
- __winfnc BOOL
- WinUsb_Initialize (HANDLE DeviceHandle, void** InterfaceHandle)
- @@ -134,6 +236,7 @@ WinUsb_Initialize (HANDLE DeviceHandle, void** InterfaceHandle)
- info->playback = 0;
-
- info->dev = NULL;
- + pthread_mutex_init(&info->reopen_lock, NULL);
-
- if(info->playback) {
- info->script = fopen("usb_script.txt", "rt");
- @@ -224,6 +327,15 @@ __winfnc BOOL WinUsb_GetDescriptor(
- LIBUSB_REQUEST_GET_DESCRIPTOR, dti,
- LanguageID, Buffer, (uint16_t) BufferLength, 1000);
-
- + //Wait for the device to come back if it re-enumerated, then retry
- + if(rc == LIBUSB_ERROR_NO_DEVICE || rc == LIBUSB_ERROR_IO) {
- + if(handle_usb_error(info, rc)) {
- + rc = libusb_control_transfer(info->dev, LIBUSB_ENDPOINT_IN,
- + LIBUSB_REQUEST_GET_DESCRIPTOR, dti,
- + LanguageID, Buffer, (uint16_t) BufferLength, 1000);
- + }
- + }
- +
-
- // if(rc >= 0) {
- // fprintf(info->script, "blackbox: usb %d dsc ", dti);
- @@ -292,7 +404,16 @@ __winfnc BOOL WinUsb_ControlTransfer(
- rc = libusb_control_transfer(info->dev,
- SetupPacket.RequestType, SetupPacket.Request, SetupPacket.Value, SetupPacket.Index,
- Buffer, SetupPacket.Length, 10000);
- -
- +
- + //Wait for the device to come back if it re-enumerated, then retry
- + if(rc == LIBUSB_ERROR_NO_DEVICE || rc == LIBUSB_ERROR_IO) {
- + if(handle_usb_error(info, rc)) {
- + rc = libusb_control_transfer(info->dev,
- + SetupPacket.RequestType, SetupPacket.Request, SetupPacket.Value, SetupPacket.Index,
- + Buffer, SetupPacket.Length, 10000);
- + }
- + }
- +
- if(rc < 0)
- return FALSE;
- #if 0
- @@ -306,7 +427,7 @@ __winfnc BOOL WinUsb_ControlTransfer(
- ULONG res = (ULONG) rc;
- *LengthTransferred = res;
- winerr_clear();
- - usleep(500000);
- + usleep(50000);
- return TRUE;
- }
- WINAPI(WinUsb_ControlTransfer)
- @@ -371,15 +492,24 @@ WinUsb_ReadPipe(
- switch(wpi->PipeType) {
- case UsbdPipeTypeBulk:
- printf("bulk xfer\n");
- - rc = libusb_bulk_transfer(
- - dev->dev,
- - PipeID,
- - Buffer,
- - BufferLength,
- - (int*)LengthTransferred,
- - dev->timeouts[PipeID]+1000);
- - //sleep(3);
- - printf("LengthTransferred: %d\n", *LengthTransferred);
- + for(;;) {
- + rc = libusb_bulk_transfer(
- + dev->dev,
- + PipeID,
- + Buffer,
- + BufferLength,
- + (int*)LengthTransferred,
- + dev->timeouts[PipeID]+1000);
- + printf("LengthTransferred: %d\n", *LengthTransferred);
- +
- + //Device re-enumerated (the driver forces this during init)?
- + //Wait for it to come back, then retry the transfer.
- + if(rc == LIBUSB_ERROR_NO_DEVICE ||
- + (rc == LIBUSB_ERROR_IO && *LengthTransferred == 0)) {
- + if(handle_usb_error(dev, rc)) continue;
- + }
- + break;
- + }
- break;
-
- case UsbdPipeTypeInterrupt:
- @@ -430,7 +560,9 @@ WinUsb_ReadPipe(
- return FALSE;
- }
-
- - exit(0); // Don't know what to do, panic quit
- + //Report the failure to the driver instead of killing the process;
- + //the driver's retry logic will handle re-initialization.
- + return FALSE;
- }
-
- return TRUE;
- @@ -508,17 +640,26 @@ WinUsb_WritePipe(
- return TRUE;
- }
-
- - rc = libusb_bulk_transfer(dev->dev,
- - PipeID,
- - Buffer,
- - BufferLength,
- - &send,
- - dev->timeouts[PipeID]+1000);
- - printf("rc=%d send=%d\n", rc, send);
- + for(;;) {
- + rc = libusb_bulk_transfer(dev->dev,
- + PipeID,
- + Buffer,
- + BufferLength,
- + &send,
- + dev->timeouts[PipeID]+1000);
- + printf("rc=%d send=%d\n", rc, send);
- +
- + //Device re-enumerated (the driver forces this during init)?
- + //Wait for it to come back, then retry the transfer.
- + if(rc == LIBUSB_ERROR_NO_DEVICE ||
- + (rc == LIBUSB_ERROR_IO && send == 0)) {
- + if(handle_usb_error(dev, rc)) continue;
- + }
- + break;
- + }
-
- if(rc < 0) {
- printf("E: xfer Failed - %s\n", libusb_error_name(rc));
- - exit(0);
- return FALSE;
- }
- *LengthTransferred = send;
- diff --git a/meson.build b/meson.build
- index af190d0..df53836 100644
- --- a/meson.build
- +++ b/meson.build
- @@ -7,6 +7,6 @@ INSTALL_DIR = '/sbin/tudor'
-
- subdir('libtudor')
- subdir('cli')
- -subdir('tudor-host')
- -subdir('tudor-host-launcher')
- -subdir('libfprint-tod')
- +# subdir('tudor-host') # needs libfprint-2-tod-1, enable once TOD fork is installed
- +# subdir('tudor-host-launcher')
- +# subdir('libfprint-tod')
- diff --git a/subprojects/cryptbridge/registry.c b/subprojects/cryptbridge/registry.c
- index b850841..dc79911 100644
- --- a/subprojects/cryptbridge/registry.c
- +++ b/subprojects/cryptbridge/registry.c
- @@ -98,7 +98,12 @@ static const char *get_base_path(void)
- // registry_base_path = strdup("/tmp/regemu");
- // }
-
- - registry_base_path = strdup("/home/rstar000/Documents/fake_reg");
- + const char *env = getenv("TUDOR_REGISTRY_PATH");
- + if (env && env[0]) {
- + registry_base_path = strdup(env);
- + } else {
- + registry_base_path = strdup("/tmp/tudor-registry");
- + }
- return registry_base_path;
- }
-
|