From 9a7275970bc82cb07d1b5d24e66e76d80682bb35 Mon Sep 17 00:00:00 2001 From: Luke A Date: Wed, 25 Sep 2024 01:12:57 -0400 Subject: [PATCH] Core0/Core1 Sync USB Stall fix (#1146) * PS5 fix for auth * What started as a simple fix has become a complete revamp of PS4/PS5 auth - Simplified auth down to a single 1064 buffer for nonce + signed nonce passing - All auth console<->dongle passing now use a single GPAuthState enum - Lots of simplifications of design for PS4 and PS4-usb-listener Still need to test against PS4 loaded keys but it should work * Fix for USB stalling * Revert sensor data back to what it was before * Update PS4Auth.h * Update PS4Driver.cpp --- headers/drivers/ps4/PS4Auth.h | 27 +-- headers/drivers/ps4/PS4AuthUSBListener.h | 36 ++-- headers/drivers/ps4/PS4Driver.h | 30 +-- headers/drivers/shared/gpauthdriver.h | 9 +- headers/drivers/xbone/XBOneAuth.h | 10 +- headers/drivers/xinput/XInputAuth.h | 8 +- src/drivers/ps4/PS4Auth.cpp | 157 +++++++------- src/drivers/ps4/PS4AuthUSBListener.cpp | 126 +++++------ src/drivers/ps4/PS4Driver.cpp | 213 ++++++++----------- src/drivers/xbone/XBOneAuth.cpp | 2 +- src/drivers/xbone/XBOneAuthUSBListener.cpp | 10 +- src/drivers/xbone/XBOneDriver.cpp | 8 +- src/drivers/xinput/XInputAuthUSBListener.cpp | 18 +- src/drivers/xinput/XInputDriver.cpp | 10 +- src/main.cpp | 2 +- 15 files changed, 307 insertions(+), 359 deletions(-) diff --git a/headers/drivers/ps4/PS4Auth.h b/headers/drivers/ps4/PS4Auth.h index fe2b0614c..12f86511b 100644 --- a/headers/drivers/ps4/PS4Auth.h +++ b/headers/drivers/ps4/PS4Auth.h @@ -2,27 +2,30 @@ #define _PS4AUTH_H_ #include "drivers/shared/gpauthdriver.h" -#include "drivers/ps4/PS4Driver.h" -#include "usblistener.h" #include "mbedtls/rsa.h" +// PS4 Auth Data in a single struct +typedef struct { + struct mbedtls_rsa_context rsa_context; + uint8_t ps4_auth_buffer[1064]; + bool valid_rsa = false; + bool dongle_ready = false; + GPAuthState passthrough_state; // PS4 Encryption Passthrough State + uint8_t nonce_id; // for nonce passing +} PS4AuthData; + class PS4Auth : public GPAuthDriver { public: PS4Auth(InputModeAuthType inType) { authType = inType; } virtual void initialize(); virtual bool available(); - void process(PS4State, uint8_t, uint8_t*); - uint8_t * getAuthBuffer() { return ps4_auth_buffer; } - bool getAuthReady(); + void process(); + PS4AuthData * getAuthData() { return &ps4AuthData; } void resetAuth(); private: - struct mbedtls_rsa_context rsa_context; - bool valid_rsa; - - // buffer = 256 + 16 + 256 + 256 + 256 + 24 - // == 1064 bytes (almost 1 kb) - uint8_t ps4_auth_buffer[1064]; - bool ps4_keys_signature_ready; + void keyModeInitialize(); + void keyModeProcess(); + PS4AuthData ps4AuthData; }; #endif diff --git a/headers/drivers/ps4/PS4AuthUSBListener.h b/headers/drivers/ps4/PS4AuthUSBListener.h index 8282423ed..5f5ac460a 100644 --- a/headers/drivers/ps4/PS4AuthUSBListener.h +++ b/headers/drivers/ps4/PS4AuthUSBListener.h @@ -3,6 +3,15 @@ #include "usblistener.h" #include "PS4Driver.h" +#include "PS4Auth.h" + +typedef enum { + no_nonce = 0, + receiving_nonce = 1, + nonce_ready = 2, + signed_nonce_ready = 3, + sending_nonce = 4 +} PS4State; class PS4AuthUSBListener : public USBListener { public: @@ -14,23 +23,22 @@ class PS4AuthUSBListener : public USBListener { virtual void report_sent(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len) {} virtual void set_report_complete(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t report_type, uint16_t len); virtual void get_report_complete(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t report_type, uint16_t len); - void process(PS4State pState, uint8_t pNonceId, uint8_t * pNonceBuffer); // add things to process - void setAuthBuffer(uint8_t * buffer) { ps4_auth_buffer = buffer; } - bool getHostAuthReady() { return ps4_auth_host_ready; } - void resetHostAuth(); + void process(); // add things to process + void setAuthData(PS4AuthData * authData) { ps4AuthData = authData; } + void resetHostData(); private: bool host_get_report(uint8_t report_id, void* report, uint16_t len); bool host_set_report(uint8_t report_id, void* report, uint16_t len); - uint8_t ps_dev_addr; - uint8_t ps_instance; - int8_t nonce_page; - PS4State passthrough_state; - int8_t send_nonce_part; - uint8_t report_buffer[64]; - bool awaiting_cb; - bool ps4_auth_host_ready; - uint8_t * ps4_auth_buffer; - bool ps_mounted; + uint8_t ps_dev_addr; // TinyUSB Address (USB) + uint8_t ps_instance; // TinyUSB Instance (USB) + PS4AuthData * ps4AuthData; // PS4 Authentication Data + uint8_t nonce_page; // PS4 Encryption Nonce Page (Max 5) + uint8_t nonce_chunk; // PS4 Encryption Nonce Chunk (Max 19) + uint8_t report_buffer[PS4_ENDPOINT_SIZE]; // Report buffer + bool awaiting_cb; // Global call-back wait + uint8_t noncelen; // process(): nonce-len + uint32_t crc32; // process(): crc32 + PS4State dongle_state; }; #endif // _PS4AUTHUSBLISTENER_H_ diff --git a/headers/drivers/ps4/PS4Driver.h b/headers/drivers/ps4/PS4Driver.h index d13d65e5c..77ba1332a 100644 --- a/headers/drivers/ps4/PS4Driver.h +++ b/headers/drivers/ps4/PS4Driver.h @@ -10,16 +10,7 @@ #include "drivers/ps4/PS4Descriptors.h" // Authentication -#include "drivers/shared/gpauthdriver.h" - -typedef enum { - no_nonce = 0, - receiving_nonce = 1, - nonce_ready = 2, - signed_nonce_ready = 3, - sending_nonce = 4, - waiting_reset = 5 -} PS4State; +#include "drivers/ps4/PS4Auth.h" typedef enum { @@ -44,7 +35,6 @@ typedef enum // 256 byte - RSA E // 256 byte - ps4 signature // 24 byte - zero padding - class PS4Driver : public GPDriver { public: PS4Driver(uint32_t type): controllerType(type) {} @@ -62,35 +52,27 @@ class PS4Driver : public GPDriver { virtual const uint8_t * get_descriptor_device_qualifier_cb(); virtual uint16_t GetJoystickMidValue(); virtual USBListener * get_usb_auth_listener(); - bool getAuthSent() { return authsent;} private: - // Lots of things here - void save_nonce(uint8_t nonce_id, uint8_t nonce_page, uint8_t * buffer, uint16_t buflen); uint8_t last_report[CFG_TUD_ENDPOINT0_SIZE] = { }; uint8_t last_report_counter; uint16_t last_axis_counter; - uint8_t cur_nonce_id; PS4Report ps4Report; TouchpadData touchpadData; PSSensorData sensorData; uint32_t last_report_timer; - uint8_t send_nonce_part; - uint32_t controllerType; - GPAuthDriver * authDriver; + PS4Auth * ps4AuthDriver; + PS4AuthData * ps4AuthData; // PS4 Authentication Data + uint8_t cur_nonce_chunk; // PS4 Encryption Nonce Chunk (Max 19) + uint8_t cur_nonce_id; + uint32_t controllerType; // PS4 DS4 / PS5 Third-Party bool pointOneTouched = false; bool pointTwoTouched = false; uint8_t touchCounter; - PS4FeatureOutputReport ps4Features; uint8_t lastFeatures[PS4_FEATURES_SIZE] = { }; - uint8_t deviceDescriptor[sizeof(ps4_device_descriptor)]; - - PS4State ps4State; bool authsent; - uint8_t nonce_buffer[256]; - uint8_t nonce_id; // used in pass-through mode }; #endif // _PS4_DRIVER_H_ diff --git a/headers/drivers/shared/gpauthdriver.h b/headers/drivers/shared/gpauthdriver.h index 76a68f78b..c487544c7 100644 --- a/headers/drivers/shared/gpauthdriver.h +++ b/headers/drivers/shared/gpauthdriver.h @@ -2,9 +2,16 @@ #define _GPAUTHDRIVER_H_ #include "enums.pb.h" - #include "usblistener.h" +typedef enum { + auth_idle_state = 0, + send_auth_console_to_dongle = 1, + send_auth_dongle_to_console = 2, + wait_auth_console_to_dongle = 3, + wait_auth_dongle_to_console = 4, +} GPAuthState; + class GPAuthDriver { public: virtual void initialize() = 0; diff --git a/headers/drivers/xbone/XBOneAuth.h b/headers/drivers/xbone/XBOneAuth.h index 2d52621d0..4b655d0e4 100644 --- a/headers/drivers/xbone/XBOneAuth.h +++ b/headers/drivers/xbone/XBOneAuth.h @@ -4,14 +4,6 @@ #include "drivers/shared/gpauthdriver.h" #include "drivers/shared/xgip_protocol.h" -typedef enum { - auth_idle_state = 0, - send_auth_console_to_dongle = 1, - send_auth_dongle_to_console = 2, - wait_auth_console_to_dongle = 3, - wait_auth_dongle_to_console = 4, -} XboxOneState; - class XBOneAuthBuffer { public: XBOneAuthBuffer() { @@ -51,7 +43,7 @@ class XBOneAuthBuffer { }; typedef struct { - XboxOneState xboneState; + GPAuthState xboneState; // Auth Buffer Queue XBOneAuthBuffer consoleBuffer; diff --git a/headers/drivers/xinput/XInputAuth.h b/headers/drivers/xinput/XInputAuth.h index d596e558f..fe7391855 100644 --- a/headers/drivers/xinput/XInputAuth.h +++ b/headers/drivers/xinput/XInputAuth.h @@ -3,12 +3,6 @@ #include "drivers/shared/gpauthdriver.h" -typedef enum { - auth_idle_state = 0, - send_auth_console_to_dongle = 1, - send_auth_dongle_to_console = 2 -} XInputAuthState; - class XInputAuthBuffer { public: XInputAuthBuffer() { @@ -49,7 +43,7 @@ class XInputAuthBuffer { // Dongle Serial 29 bytes // Console-Dongle Back and Forth 46 bytes & 22 bytes typedef struct { - XInputAuthState xinputState; + GPAuthState xinputState; uint8_t consoleInitialAuth[X360_AUTHLEN_CONSOLE_INIT]; // Console Init (Keep when Dongle Reboots) uint8_t dongleSerial[X360_AUTHLEN_DONGLE_SERIAL]; // Dongle Serial uint8_t passthruBuffer[X360_AUTHLEN_DONGLE_INIT]; // Back-and-Forth Buffer (46 or 22 bytes) diff --git a/src/drivers/ps4/PS4Auth.cpp b/src/drivers/ps4/PS4Auth.cpp index ee9c2ffca..4e2b91180 100644 --- a/src/drivers/ps4/PS4Auth.cpp +++ b/src/drivers/ps4/PS4Auth.cpp @@ -32,33 +32,17 @@ void PS4Auth::initialize() { return; } - memset(ps4_auth_buffer, 0, 1064); // we might not need to 0 + listener = nullptr; + ps4AuthData.dongle_ready = false; + ps4AuthData.nonce_id = 0; + ps4AuthData.passthrough_state = GPAuthState::auth_idle_state; + memset(ps4AuthData.ps4_auth_buffer, 0, 1064); if (authType == InputModeAuthType::INPUT_MODE_AUTH_TYPE_KEYS ) { - const PS4Options& options = Storage::getInstance().getAddonOptions().ps4Options; - valid_rsa = false; - NEW_CONFIG_MPI(N, options.rsaN.bytes, options.rsaN.size) - NEW_CONFIG_MPI(E, options.rsaE.bytes, options.rsaE.size) - NEW_CONFIG_MPI(P, options.rsaP.bytes, options.rsaP.size) - NEW_CONFIG_MPI(Q, options.rsaQ.bytes, options.rsaQ.size) - mbedtls_rsa_init(&rsa_context, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256); - if (mbedtls_rsa_import(&rsa_context, &N, &P, &Q, nullptr, &E) == 0 && - mbedtls_rsa_complete(&rsa_context) == 0) { - valid_rsa = true; - } - DELETE_CONFIG_MPI(N) - DELETE_CONFIG_MPI(E) - DELETE_CONFIG_MPI(P) - DELETE_CONFIG_MPI(Q) - ps4_keys_signature_ready = false; - listener = nullptr; - - // Generate some random for RNG - srand(getMillis()); + keyModeInitialize(); } else if (authType == InputModeAuthType::INPUT_MODE_AUTH_TYPE_USB ) { - PS4AuthUSBListener * newListener = new PS4AuthUSBListener(); - newListener->setup(); - newListener->setAuthBuffer(ps4_auth_buffer); - listener = newListener; + listener = new PS4AuthUSBListener(); + ((PS4AuthUSBListener*)listener)->setup(); + ((PS4AuthUSBListener*)listener)->setAuthData(&ps4AuthData); } } @@ -78,71 +62,82 @@ bool PS4Auth::available() { return false; } -void PS4Auth::process(PS4State pState, uint8_t pNonceId, uint8_t * pNonceBuffer) { +void PS4Auth::process() { if (authType == InputModeAuthType::INPUT_MODE_AUTH_TYPE_KEYS ) { - // Do not run if RSA is invalid - if (!valid_rsa) { - return; - } - - // Check to see if the PS4 Authentication needs work - if ( pState == PS4State::nonce_ready && ps4_keys_signature_ready == false ) { - const PS4Options& options = Storage::getInstance().getAddonOptions().ps4Options; - int rss_error = 0; - - uint8_t hashed_nonce[32]; - - // Sign the nonce now that we got it! - // - if ( mbedtls_sha256_ret(pNonceBuffer, 256, hashed_nonce, 0) < 0 ) { - return; - } + keyModeProcess(); + } else if (authType == InputModeAuthType::INPUT_MODE_AUTH_TYPE_USB ) { + ((PS4AuthUSBListener*)listener)->process(); // process HOST with client data + } +} - rss_error = mbedtls_rsa_rsassa_pss_sign(&rsa_context, rng, nullptr, - MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA256, - 32, hashed_nonce, - ps4_auth_buffer); +// Init if we're in ps4 key mode +void PS4Auth::keyModeInitialize() { + ps4AuthData.valid_rsa = false; + const PS4Options& options = Storage::getInstance().getAddonOptions().ps4Options; + NEW_CONFIG_MPI(N, options.rsaN.bytes, options.rsaN.size) + NEW_CONFIG_MPI(E, options.rsaE.bytes, options.rsaE.size) + NEW_CONFIG_MPI(P, options.rsaP.bytes, options.rsaP.size) + NEW_CONFIG_MPI(Q, options.rsaQ.bytes, options.rsaQ.size) + mbedtls_rsa_init(&ps4AuthData.rsa_context, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256); + if (mbedtls_rsa_import(&ps4AuthData.rsa_context, &N, &P, &Q, nullptr, &E) == 0 && + mbedtls_rsa_complete(&ps4AuthData.rsa_context) == 0) { + ps4AuthData.valid_rsa = true; + } + DELETE_CONFIG_MPI(N) + DELETE_CONFIG_MPI(E) + DELETE_CONFIG_MPI(P) + DELETE_CONFIG_MPI(Q) - if ( rss_error < 0 ) { - return; - } - // copy the parts into our authentication buffer - size_t offset = 256; - memcpy(&ps4_auth_buffer[offset], options.serial.bytes, 16); - offset += 16; - mbedtls_rsa_export_raw( - &rsa_context, - &ps4_auth_buffer[offset], 256, - nullptr, 0, - nullptr, 0, - nullptr, 0, - &ps4_auth_buffer[offset+256], 256 - ); - offset += 512; - memcpy(&ps4_auth_buffer[offset], options.signature.bytes, 256); - offset += 256; - memset(&ps4_auth_buffer[offset], 0, 24); + // Reset our random seed + srand(0); +} - ps4_keys_signature_ready = true; // auth buffer is ready - } - } else if (authType == InputModeAuthType::INPUT_MODE_AUTH_TYPE_USB ) { - ((PS4AuthUSBListener*)listener)->process(pState, pNonceId, pNonceBuffer); // process HOST with client data +// Process if we are using ps4 keys +void PS4Auth::keyModeProcess() { + // Do not run if RSA is invalid + if (!ps4AuthData.valid_rsa) { + return; } -} -bool PS4Auth::getAuthReady() { - if (authType == InputModeAuthType::INPUT_MODE_AUTH_TYPE_KEYS ) { - return ps4_keys_signature_ready; - } else if (authType == InputModeAuthType::INPUT_MODE_AUTH_TYPE_USB ) { - return ((PS4AuthUSBListener*)listener)->getHostAuthReady(); + // Check to see if the PS4 Authentication needs work + if ( ps4AuthData.passthrough_state == GPAuthState::send_auth_console_to_dongle ) { + const PS4Options& options = Storage::getInstance().getAddonOptions().ps4Options; + int rss_error = 0; + uint8_t hashed_nonce[32]; + // Sign our nonce into hashed_nonce + if ( mbedtls_sha256_ret(ps4AuthData.ps4_auth_buffer, 256, hashed_nonce, 0) < 0 ) { + return; + } + rss_error = mbedtls_rsa_rsassa_pss_sign(&ps4AuthData.rsa_context, rng, nullptr, + MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA256, + 32, hashed_nonce, + ps4AuthData.ps4_auth_buffer); + if ( rss_error < 0 ) { + return; // If we could not sign with our key, return (error) + } + // copy the parts into our authentication buffer + size_t offset = 256; + memcpy(&ps4AuthData.ps4_auth_buffer[offset], options.serial.bytes, 16); + offset += 16; + mbedtls_rsa_export_raw( + &ps4AuthData.rsa_context, + &ps4AuthData.ps4_auth_buffer[offset], 256, + nullptr, 0, + nullptr, 0, + nullptr, 0, + &ps4AuthData.ps4_auth_buffer[offset+256], 256 + ); + offset += 512; + memcpy(&ps4AuthData.ps4_auth_buffer[offset], options.signature.bytes, 256); + offset += 256; + memset(&ps4AuthData.ps4_auth_buffer[offset], 0, 24); + ps4AuthData.passthrough_state = GPAuthState::send_auth_dongle_to_console; } - return false; } void PS4Auth::resetAuth() { - if (authType == InputModeAuthType::INPUT_MODE_AUTH_TYPE_KEYS ) { - ps4_keys_signature_ready = false; - } else if (authType == InputModeAuthType::INPUT_MODE_AUTH_TYPE_USB ) { - ((PS4AuthUSBListener*)listener)->resetHostAuth(); + if (authType == InputModeAuthType::INPUT_MODE_AUTH_TYPE_USB ) { + ((PS4AuthUSBListener*)listener)->resetHostData(); } + ps4AuthData.passthrough_state = GPAuthState::auth_idle_state; } diff --git a/src/drivers/ps4/PS4AuthUSBListener.cpp b/src/drivers/ps4/PS4AuthUSBListener.cpp index 63429915c..add76b982 100644 --- a/src/drivers/ps4/PS4AuthUSBListener.cpp +++ b/src/drivers/ps4/PS4AuthUSBListener.cpp @@ -9,143 +9,131 @@ static const uint8_t output_0xf3[] = { 0x0, 0x38, 0x38, 0, 0, 0, 0 }; void PS4AuthUSBListener::setup() { - nonce_page = 0; // no nonce yet - send_nonce_part = 0; // which part of the nonce are we getting from send? - awaiting_cb = false; // did we receive the sign state yet - passthrough_state = PS4State::no_nonce; - ps4_auth_host_ready = false; - ps4_auth_buffer = nullptr; ps_dev_addr = 0xFF; ps_instance = 0xFF; - ps_mounted = false; + ps4AuthData = nullptr; + resetHostData(); } -void PS4AuthUSBListener::process(PS4State pState, uint8_t pNonceId, uint8_t * pNonceBuffer) { - if ( awaiting_cb == true) +void PS4AuthUSBListener::process() { + if ( awaiting_cb == true || ps4AuthData == nullptr ) return; - uint8_t noncelen; - uint32_t crc32; - uint8_t buffer[64]; // [0xF0, ID, Page, 0, nonce(54 or 32 with 0 padding), CRC32 of data] - - switch ( passthrough_state ) { + switch ( dongle_state ) { case PS4State::no_nonce: - // Did we get the nonce? Let's begin auth - if ( pState == nonce_ready ) { - ps4_auth_host_ready = false; + // Once Console is ready with the nonce, begin! + if ( ps4AuthData->passthrough_state == GPAuthState::send_auth_console_to_dongle ) { memcpy(report_buffer, output_0xf3, sizeof(output_0xf3)); host_get_report(PS4AuthReport::PS4_RESET_AUTH, report_buffer, sizeof(output_0xf3)); } break; case PS4State::receiving_nonce: - buffer[0] = PS4AuthReport::PS4_SET_AUTH_PAYLOAD; // [0xF0, ID, Page, 0, nonce(54 or 32 with 0 padding), CRC32 of data] - buffer[1] = pNonceId; - buffer[2] = nonce_page; - buffer[3] = 0; + report_buffer[0] = PS4AuthReport::PS4_SET_AUTH_PAYLOAD; // [0xF0, ID, Page, 0, nonce(54 or 32 with 0 padding), CRC32 of data] + report_buffer[1] = ps4AuthData->nonce_id; + report_buffer[2] = nonce_page; + report_buffer[3] = 0; if ( nonce_page == 4 ) { noncelen = 32; // from 4 to 64 - 24 - 4 - memcpy(&buffer[4], &pNonceBuffer[nonce_page*56], noncelen); - memset(&buffer[4+noncelen], 0, 24); // zero padding + memcpy(&report_buffer[4], &ps4AuthData->ps4_auth_buffer[nonce_page*56], noncelen); + memset(&report_buffer[4+noncelen], 0, 24); // zero padding } else { noncelen = 56; - memcpy(&buffer[4], &pNonceBuffer[nonce_page*56], noncelen); + memcpy(&report_buffer[4], &ps4AuthData->ps4_auth_buffer[nonce_page*56], noncelen); } nonce_page++; - crc32 = CRC32::calculate(buffer, 60); - memcpy(&buffer[60], &crc32, sizeof(uint32_t)); - memcpy(report_buffer, buffer, 64); + crc32 = CRC32::calculate(report_buffer, 60); + memcpy(&report_buffer[60], &crc32, sizeof(uint32_t)); host_set_report(PS4AuthReport::PS4_SET_AUTH_PAYLOAD, report_buffer, 64); break; case PS4State::signed_nonce_ready: - buffer[0] = PS4AuthReport::PS4_GET_SIGNING_STATE; - buffer[1] = pNonceId; - memset(&buffer[2], 0, 14); - memcpy(report_buffer, buffer, 16); + report_buffer[0] = PS4AuthReport::PS4_GET_SIGNING_STATE; + report_buffer[1] = ps4AuthData->nonce_id; + memset(&report_buffer[2], 0, 14); host_get_report(PS4AuthReport::PS4_GET_SIGNING_STATE, report_buffer, 16); break; case PS4State::sending_nonce: - buffer[0] = PS4AuthReport::PS4_GET_SIGNATURE_NONCE; - buffer[1] = pNonceId; // nonce_id - buffer[2] = send_nonce_part; // next_part - memset(&buffer[3], 0, 61); // zero rest of memory - memcpy(report_buffer, buffer, 64); - send_nonce_part++; // Nonce Part is reset during callback + report_buffer[0] = PS4AuthReport::PS4_GET_SIGNATURE_NONCE; + report_buffer[1] = ps4AuthData->nonce_id; // nonce_id + report_buffer[2] = nonce_chunk; // next_part + memset(&report_buffer[3], 0, 61); // zero rest of memory + nonce_chunk++; // Nonce Part is reset during callback host_get_report(PS4AuthReport::PS4_GET_SIGNATURE_NONCE, report_buffer, 64); break; - case PS4State::waiting_reset: default: break; }; } -void PS4AuthUSBListener::resetHostAuth() { +void PS4AuthUSBListener::resetHostData() { nonce_page = 0; // no nonce yet - send_nonce_part = 0; // which part of the nonce are we getting from send? - passthrough_state = PS4State::no_nonce; + nonce_chunk = 0; // which part of the nonce are we getting from send? + awaiting_cb = false; + dongle_state = PS4State::no_nonce; } bool PS4AuthUSBListener::host_get_report(uint8_t report_id, void* report, uint16_t len) { awaiting_cb = true; - return tuh_hid_get_report(ps_dev_addr, ps_instance, report_id, HID_REPORT_TYPE_FEATURE, report, len); + return tuh_hid_get_report(ps_dev_addr, ps_instance, report_id, HID_REPORT_TYPE_FEATURE, report, len); } bool PS4AuthUSBListener::host_set_report(uint8_t report_id, void* report, uint16_t len) { awaiting_cb = true; - return tuh_hid_set_report(ps_dev_addr, ps_instance, report_id, HID_REPORT_TYPE_FEATURE, report, len); + return tuh_hid_set_report(ps_dev_addr, ps_instance, report_id, HID_REPORT_TYPE_FEATURE, report, len); } void PS4AuthUSBListener::mount(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len) { // Prevent Magic-X double mount - if ( ps_mounted == true ) + if ( ps4AuthData->dongle_ready == true ) { return; + } // Only a PS4 interface has vendor IDs F0, F1, F2, and F3 tuh_hid_report_info_t report_info[4]; uint8_t report_count = tuh_hid_parse_report_descriptor(report_info, 4, desc_report, desc_len); - bool ps4Dongle = false; + bool isPS4Dongle = false; for(uint8_t i = 0; i < report_count; i++) { if ( report_info[i].usage_page == 0xFFF0 && (report_info[i].report_id == 0xF3) ) { - ps4Dongle = true; + isPS4Dongle = true; break; } } - if (ps4Dongle == false ) + if (isPS4Dongle == false ) return; ps_dev_addr = dev_addr; ps_instance = instance; - ps_mounted = true; + ps4AuthData->dongle_ready = true; // Reset as soon as its connected - memset(report_buffer, 0, sizeof(report_buffer)); + memset(report_buffer, 0, PS4_ENDPOINT_SIZE); report_buffer[0] = PS4AuthReport::PS4_DEFINITION; host_get_report(PS4AuthReport::PS4_DEFINITION, report_buffer, 48); } void PS4AuthUSBListener::unmount(uint8_t dev_addr) { - if ( ps_mounted == false || dev_addr != ps_dev_addr ) + if ( ps4AuthData->dongle_ready == false || + (dev_addr != ps_dev_addr) ) { return; - - nonce_page = 0; // no nonce yet - send_nonce_part = 0; // which part of the nonce are we getting from send? - awaiting_cb = false; // did we receive the sign state yet - passthrough_state = PS4State::no_nonce; - ps4_auth_host_ready = false; + } + ps_dev_addr = 0xFF; ps_instance = 0xFF; - ps_mounted = false; + resetHostData(); + ps4AuthData->dongle_ready = false; } void PS4AuthUSBListener::set_report_complete(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t report_type, uint16_t len) { - if ( ps_mounted == false || dev_addr != ps_dev_addr || instance != ps_instance ) + if ( ps4AuthData->dongle_ready == false || + (dev_addr != ps_dev_addr) || (instance != ps_instance) ) { return; + } switch(report_id) { case PS4AuthReport::PS4_SET_AUTH_PAYLOAD: if (nonce_page == 5) { nonce_page = 0; - passthrough_state = PS4State::signed_nonce_ready; + dongle_state = PS4State::signed_nonce_ready; } break; default: @@ -155,26 +143,30 @@ void PS4AuthUSBListener::set_report_complete(uint8_t dev_addr, uint8_t instance, } void PS4AuthUSBListener::get_report_complete(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t report_type, uint16_t len) { - if ( ps_mounted == false || dev_addr != ps_dev_addr || instance != ps_instance ) + if ( ps4AuthData->dongle_ready == false || + (dev_addr != ps_dev_addr) || (instance != ps_instance) ) { return; + } switch(report_id) { case PS4AuthReport::PS4_DEFINITION: break; case PS4AuthReport::PS4_RESET_AUTH: - passthrough_state = PS4State::receiving_nonce; + nonce_page = 0; + nonce_chunk = 0; + dongle_state = PS4State::receiving_nonce; break; case PS4AuthReport::PS4_GET_SIGNING_STATE: if (report_buffer[2] == 0) // 0 = ready, 1 = error in signing, 16 = not ready - passthrough_state = PS4State::sending_nonce; + dongle_state = PS4State::sending_nonce; break; case PS4AuthReport::PS4_GET_SIGNATURE_NONCE: // probably should mutex lock - memcpy(&ps4_auth_buffer[(send_nonce_part-1)*56], &report_buffer[4], 56); - if (send_nonce_part == 19) { - send_nonce_part = 0; - passthrough_state = PS4State::waiting_reset; // something we don't support - ps4_auth_host_ready = true; + memcpy(&ps4AuthData->ps4_auth_buffer[(nonce_chunk-1)*56], &report_buffer[4], 56); + if (nonce_chunk == 19) { + nonce_chunk = 0; + dongle_state = PS4State::no_nonce; // something we don't support + ps4AuthData->passthrough_state = GPAuthState::send_auth_dongle_to_console; } break; default: diff --git a/src/drivers/ps4/PS4Driver.cpp b/src/drivers/ps4/PS4Driver.cpp index e25b460d0..41c09e721 100644 --- a/src/drivers/ps4/PS4Driver.cpp +++ b/src/drivers/ps4/PS4Driver.cpp @@ -16,6 +16,48 @@ // force a report to be sent every X ms #define PS4_KEEPALIVE_TIMER 5 +// Controller calibration +static constexpr uint8_t output_0x02[] = { + 0xfe, 0xff, 0x0e, 0x00, 0x04, 0x00, 0xd4, 0x22, + 0x2a, 0xdd, 0xbb, 0x22, 0x5e, 0xdd, 0x81, 0x22, + 0x84, 0xdd, 0x1c, 0x02, 0x1c, 0x02, 0x85, 0x1f, + 0xb0, 0xe0, 0xc6, 0x20, 0xb5, 0xe0, 0xb1, 0x20, + 0x83, 0xdf, 0x0c, 0x00 +}; + +// Controller descriptor (byte[4] = 0x00 for ps4, 0x07 for ps5) +static constexpr uint8_t output_0x03[] = { + 0x21, 0x27, 0x04, 0xcf, 0x00, 0x2c, 0x56, + 0x08, 0x00, 0x3d, 0x00, 0xe8, 0x03, 0x04, 0x00, + 0xff, 0x7f, 0x0d, 0x0d, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; + +// Bluetooth device and host details +static constexpr uint8_t output_0x12[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // device MAC address + 0x08, 0x25, 0x00, // BT device class + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // host MAC address +}; + +// Controller firmware version and datestamp +static constexpr uint8_t output_0xa3[] = { + 0x4a, 0x75, 0x6e, 0x20, 0x20, 0x39, 0x20, 0x32, + 0x30, 0x31, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x31, 0x32, 0x3a, 0x33, 0x36, 0x3a, 0x34, 0x31, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x01, 0x08, 0xb4, 0x01, 0x00, 0x00, 0x00, + 0x07, 0xa0, 0x10, 0x20, 0x00, 0xa0, 0x02, 0x00 +}; + +// Nonce Page Size: 0x38 (56) +// Response Page Size: 0x38 (56) +static constexpr uint8_t output_0xf3[] = { + 0x0, 0x38, 0x38, 0, 0, 0, 0 +}; + void PS4Driver::initialize() { Gamepad * gamepad = Storage::getInstance().GetGamepad(); const GamepadOptions & options = gamepad->getOptions(); @@ -93,30 +135,27 @@ void PS4Driver::initialize() { .sof = NULL }; - last_report_counter = 0; + last_report_counter = 0; // PS4 Reports last_axis_counter = 0; - cur_nonce_id = 1; last_report_timer = to_ms_since_boot(get_absolute_time()); - send_nonce_part = 0; - - // for PS4 encryption - ps4State = PS4State::no_nonce; - authsent = false; - memset(nonce_buffer, 0, 256); + cur_nonce_id = 1; // PS4 Auth + cur_nonce_chunk = 0; } void PS4Driver::initializeAux() { - authDriver = nullptr; + ps4AuthDriver = nullptr; GamepadOptions & gamepadOptions = Storage::getInstance().getGamepadOptions(); if ( controllerType == PS4ControllerType::PS4_CONTROLLER ) { - authDriver = new PS4Auth(gamepadOptions.ps4AuthType); + ps4AuthDriver = new PS4Auth(gamepadOptions.ps4AuthType); } else if ( controllerType == PS4ControllerType::PS4_ARCADESTICK ) { // Setup PS5 Auth System - authDriver = new PS4Auth(gamepadOptions.ps5AuthType); + ps4AuthDriver = new PS4Auth(gamepadOptions.ps5AuthType); } // If authentication driver is set AND auth driver can load (usb enabled, i2c enabled, keys loaded, etc.) - if ( authDriver != nullptr && authDriver->available() ) { - authDriver->initialize(); + if ( ps4AuthDriver != nullptr && ps4AuthDriver->available() ) { + ps4AuthDriver->initialize(); + ps4AuthData = ps4AuthDriver->getAuthData(); + authsent = false; } } @@ -306,58 +345,18 @@ void PS4Driver::process(Gamepad * gamepad) { // Called by Core1, PS4 key signing will lock the CPU void PS4Driver::processAux() { // If authentication driver is set AND auth driver can load (usb enabled, i2c enabled, keys loaded, etc.) - if ( authDriver != nullptr && authDriver->available() ) { - ((PS4Auth*)authDriver)->process(ps4State, nonce_id, nonce_buffer); + if ( ps4AuthDriver != nullptr && ps4AuthDriver->available() ) { + ps4AuthDriver->process(); } } USBListener * PS4Driver::get_usb_auth_listener() { - if ( authDriver != nullptr && authDriver->getAuthType() == InputModeAuthType::INPUT_MODE_AUTH_TYPE_USB ) { - return authDriver->getListener();; + if ( ps4AuthDriver != nullptr && ps4AuthDriver->getAuthType() == InputModeAuthType::INPUT_MODE_AUTH_TYPE_USB ) { + return ps4AuthDriver->getListener(); } return nullptr; } -// Controller calibration -static constexpr uint8_t output_0x02[] = { - 0xfe, 0xff, 0x0e, 0x00, 0x04, 0x00, 0xd4, 0x22, - 0x2a, 0xdd, 0xbb, 0x22, 0x5e, 0xdd, 0x81, 0x22, - 0x84, 0xdd, 0x1c, 0x02, 0x1c, 0x02, 0x85, 0x1f, - 0xb0, 0xe0, 0xc6, 0x20, 0xb5, 0xe0, 0xb1, 0x20, - 0x83, 0xdf, 0x0c, 0x00 -}; - -// Controller descriptor (byte[4] = 0x00 for ps4, 0x07 for ps5) -static constexpr uint8_t output_0x03[] = { - 0x21, 0x27, 0x04, 0xcf, 0x00, 0x2c, 0x56, - 0x08, 0x00, 0x3d, 0x00, 0xe8, 0x03, 0x04, 0x00, - 0xff, 0x7f, 0x0d, 0x0d, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 -}; - -// Bluetooth device and host details -static constexpr uint8_t output_0x12[] = { - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // device MAC address - 0x08, 0x25, 0x00, // BT device class - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // host MAC address -}; - -// Controller firmware version and datestamp -static constexpr uint8_t output_0xa3[] = { - 0x4a, 0x75, 0x6e, 0x20, 0x20, 0x39, 0x20, 0x32, - 0x30, 0x31, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x31, 0x32, 0x3a, 0x33, 0x36, 0x3a, 0x34, 0x31, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x01, 0x08, 0xb4, 0x01, 0x00, 0x00, 0x00, - 0x07, 0xa0, 0x10, 0x20, 0x00, 0xa0, 0x02, 0x00 -}; - -// Nonce Page Size: 0x38 (56) -// Response Page Size: 0x38 (56) -static constexpr uint8_t output_0xf3[] = { 0x0, 0x38, 0x38, 0, 0, 0, 0 }; - // tud_hid_get_report_cb uint16_t PS4Driver::get_report(uint8_t report_id, hid_report_type_t report_type, uint8_t *buffer, uint16_t reqlen) { if ( report_type != HID_REPORT_TYPE_FEATURE ) { @@ -365,13 +364,14 @@ uint16_t PS4Driver::get_report(uint8_t report_id, hid_report_type_t report_type, return sizeof(ps4Report); } - PS4Auth * ps4AuthDriver = (PS4Auth*)authDriver; - uint8_t * ps4_auth_buffer = ps4AuthDriver->getAuthBuffer(); - bool ps4_auth_buffer_ready = ps4AuthDriver->getAuthReady(); + // Do nothing if we do not have host authentication data or a driver to run on + if ( ps4AuthData == nullptr || ps4AuthDriver == nullptr) { + return sizeof(ps4Report); + } + uint8_t data[64] = {}; uint32_t crc32; uint16_t responseLen = 0; - //ps4_out_buffer[0] = report_id; switch(report_id) { // Controller Definition Report case PS4AuthReport::PS4_GET_CALIBRATION: @@ -408,45 +408,42 @@ uint16_t PS4Driver::get_report(uint8_t report_id, hid_report_type_t report_type, // We send 56 byte chunks back to the PS4, we've already calculated these data[0] = 0xF1; data[1] = cur_nonce_id; // nonce_id - data[2] = send_nonce_part; // next_part + data[2] = cur_nonce_chunk; // next_part data[3] = 0; // 56 byte chunks - memcpy(&data[4], &ps4_auth_buffer[send_nonce_part*56], 56); + memcpy(&data[4], &ps4AuthData->ps4_auth_buffer[cur_nonce_chunk*56], 56); // calculate the CRC32 of the buffer and write it back crc32 = CRC32::calculate(data, 60); memcpy(&data[60], &crc32, sizeof(uint32_t)); memcpy(buffer, &data[1], 63); // move data over to buffer - if ( (++send_nonce_part) == 19 ) { - ps4State = PS4State::no_nonce; + cur_nonce_chunk++; + if ( cur_nonce_chunk == 19 ) { // done! + ps4AuthData->passthrough_state = GPAuthState::auth_idle_state; authsent = true; - send_nonce_part = 0; + cur_nonce_chunk = 0; } return 63; - // Are we ready to sign? - case PS4AuthReport::PS4_GET_SIGNING_STATE: - data[0] = 0xF2; + case PS4AuthReport::PS4_GET_SIGNING_STATE: // Are we ready to sign? + data[0] = 0xF2; data[1] = cur_nonce_id; - data[2] = ps4_auth_buffer_ready == true ? 0 : 16; // 0 means auth is ready, 16 means we're still signing + data[2] = (ps4AuthData->passthrough_state == GPAuthState::send_auth_dongle_to_console) ? 0 : 16; // 0 means auth is ready, 16 means we're still signing memset(&data[3], 0, 9); crc32 = CRC32::calculate(data, 12); memcpy(&data[12], &crc32, sizeof(uint32_t)); memcpy(buffer, &data[1], 15); // move data over to buffer return 15; - case PS4AuthReport::PS4_RESET_AUTH: // Reset the Authentication + case PS4AuthReport::PS4_RESET_AUTH: // Reset the Authentication if (reqlen < sizeof(output_0xf3)) { return -1; } responseLen = MAX(reqlen, sizeof(output_0xf3)); memcpy(buffer, output_0xf3, responseLen); - ps4State = PS4State::no_nonce; - if ( authDriver != nullptr ) { - ((PS4Auth*)authDriver)->resetAuth(); // reset the auth driver if it exists - } + ps4AuthData->passthrough_state = GPAuthState::auth_idle_state; + ps4AuthDriver->resetAuth(); // reset our auth driver (ps4 keys or usb host) return responseLen; default: - //printf("[Data Undefined]\n"); break; }; return -1; @@ -462,68 +459,46 @@ void PS4Driver::set_report(uint8_t report_id, hid_report_type_t report_type, uin memcpy(&ps4Features, buffer, bufsize); } } else if (report_type == HID_REPORT_TYPE_FEATURE) { - uint8_t nonce_id; - uint8_t nonce_page; - uint32_t crc32; - uint8_t sendBuffer[64]; - uint8_t nonce[56]; // max nonce data - uint16_t noncelen; - uint16_t buflen; - if (report_id == PS4AuthReport::PS4_SET_HOST_MAC) { // } else if (report_id == PS4AuthReport::PS4_SET_USB_BT_CONTROL) { // } else if (report_id == PS4AuthReport::PS4_SET_AUTH_PAYLOAD) { + uint8_t sendBuffer[64]; + uint8_t nonce_id; + uint8_t nonce_page; + uint16_t buflen; if (bufsize != 63 ) { return; } - - // Setup CRC32 buffer + // Calculate CRC32 of buffer sendBuffer[0] = report_id; memcpy(&sendBuffer[1], buffer, bufsize); buflen = bufsize + 1; - + if ( CRC32::calculate(sendBuffer, buflen-sizeof(uint32_t)) != + *((unsigned int*)&sendBuffer[buflen-sizeof(uint32_t)])) { + return; // CRC32 failed on set report + } + // 256 byte nonce, 4 56-byte chunks, 1 32-byte chunk nonce_id = buffer[0]; nonce_page = buffer[1]; - // data[2] is zero padding - - crc32 = CRC32::calculate(sendBuffer, buflen-sizeof(uint32_t)); - if ( crc32 != *((unsigned int*)&sendBuffer[buflen-sizeof(uint32_t)])) { - return; // CRC32 failed on set report + if ( nonce_page == 4 ) { // Nonce page 4 : 32 bytes + memcpy(&ps4AuthData->ps4_auth_buffer[nonce_page*56], &sendBuffer[4], 32); + ps4AuthData->nonce_id = nonce_id; + ps4AuthData->passthrough_state = GPAuthState::send_auth_console_to_dongle; + } else { // Nonce page 0-3 : 56 bytes + memcpy(&ps4AuthData->ps4_auth_buffer[nonce_page*56], &sendBuffer[4], 56); } - - // 256 byte nonce, with 56 byte packets leaves 24 extra bytes on the last packet? - if ( nonce_page == 4 ) { - // Copy/append data from buffer[4:64-28] into our nonce - noncelen = 32; // from 4 to 64 - 24 - 4 - nonce_id = nonce_id; // for pass-through only - } else { - // Copy/append data from buffer[4:64-4] into our nonce - noncelen = 56; - // from 4 to 64 - 4 + if ( nonce_page == 0 ) { // Set our passthrough state on first nonce + cur_nonce_id = nonce_id; // update current nonce ID + } else if ( nonce_id != cur_nonce_id ) { + // ERROR: Nonce ID is incorrect + ps4AuthData->passthrough_state = GPAuthState::auth_idle_state; } - - memcpy(nonce, &sendBuffer[4], noncelen); - save_nonce(nonce_id, nonce_page, nonce, noncelen); } } } -void PS4Driver::save_nonce(uint8_t nonce_id, uint8_t nonce_page, uint8_t * buffer, uint16_t buflen) { - if ( nonce_page != 0 && nonce_id != cur_nonce_id ) { - ps4State = PS4State::no_nonce; - return; // setting nonce with mismatched id - } - - memcpy(&nonce_buffer[nonce_page*56], buffer, buflen); - if ( nonce_page == 4 ) { - ps4State = PS4State::nonce_ready; - } else if ( nonce_page == 0 ) { - cur_nonce_id = nonce_id; - ps4State = PS4State::receiving_nonce; - } -} // Only XboxOG and Xbox One use vendor control xfer cb bool PS4Driver::vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const *request) { diff --git a/src/drivers/xbone/XBOneAuth.cpp b/src/drivers/xbone/XBOneAuth.cpp index b4293b81a..45e1c2827 100644 --- a/src/drivers/xbone/XBOneAuth.cpp +++ b/src/drivers/xbone/XBOneAuth.cpp @@ -14,7 +14,7 @@ void XBOneAuth::initialize() { if ( available() ) { listener = new XBOneAuthUSBListener(); - xboxOneAuthData.xboneState = auth_idle_state; + xboxOneAuthData.xboneState = GPAuthState::auth_idle_state; xboxOneAuthData.authCompleted = false; ((XBOneAuthUSBListener*)listener)->setup(); ((XBOneAuthUSBListener*)listener)->setAuthData(&xboxOneAuthData); diff --git a/src/drivers/xbone/XBOneAuthUSBListener.cpp b/src/drivers/xbone/XBOneAuthUSBListener.cpp index 43cb0fabc..4ffade869 100644 --- a/src/drivers/xbone/XBOneAuthUSBListener.cpp +++ b/src/drivers/xbone/XBOneAuthUSBListener.cpp @@ -44,7 +44,7 @@ void XBOneAuthUSBListener::process() { return; // Received a packet from the console (or Windows) to dongle - if ( xboxOneAuthData->xboneState == XboxOneState::send_auth_console_to_dongle ) { + if ( xboxOneAuthData->xboneState == GPAuthState::send_auth_console_to_dongle ) { uint8_t isChunked = ( xboxOneAuthData->consoleBuffer.length > GIP_MAX_CHUNK_SIZE ); uint8_t needsAck = ( xboxOneAuthData->consoleBuffer.length > 2 ); outgoingXGIP.reset(); @@ -52,14 +52,14 @@ void XBOneAuthUSBListener::process() { xboxOneAuthData->consoleBuffer.sequence, 1, isChunked, needsAck); outgoingXGIP.setData(xboxOneAuthData->consoleBuffer.data, xboxOneAuthData->consoleBuffer.length); xboxOneAuthData->consoleBuffer.reset(); - xboxOneAuthData->xboneState = XboxOneState::wait_auth_console_to_dongle; + xboxOneAuthData->xboneState = GPAuthState::wait_auth_console_to_dongle; } // Process waiting (always on first frame) - if ( xboxOneAuthData->xboneState == XboxOneState::wait_auth_console_to_dongle) { + if ( xboxOneAuthData->xboneState == GPAuthState::wait_auth_console_to_dongle) { queue_host_report(outgoingXGIP.generatePacket(), outgoingXGIP.getPacketLength()); if ( outgoingXGIP.getChunked() == false || outgoingXGIP.endOfChunk() == true) { - xboxOneAuthData->xboneState = XboxOneState::auth_idle_state; + xboxOneAuthData->xboneState = GPAuthState::auth_idle_state; } } @@ -139,7 +139,7 @@ void XBOneAuthUSBListener::report_received(uint8_t dev_addr, uint8_t instance, u (incomingXGIP.getChunked() == true && incomingXGIP.endOfChunk() == true )) { xboxOneAuthData->dongleBuffer.setBuffer(incomingXGIP.getData(), incomingXGIP.getDataLength(), incomingXGIP.getSequence(), incomingXGIP.getCommand()); - xboxOneAuthData->xboneState = XboxOneState::send_auth_dongle_to_console; + xboxOneAuthData->xboneState = GPAuthState::send_auth_dongle_to_console; incomingXGIP.reset(); } break; diff --git a/src/drivers/xbone/XBOneDriver.cpp b/src/drivers/xbone/XBOneDriver.cpp index 41dff9cc8..e3c3c130d 100644 --- a/src/drivers/xbone/XBOneDriver.cpp +++ b/src/drivers/xbone/XBOneDriver.cpp @@ -282,7 +282,7 @@ bool xbone_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, (incomingXGIP->getChunked() == false )) { xboxOneAuthData->consoleBuffer.setBuffer(incomingXGIP->getData(), incomingXGIP->getDataLength(), incomingXGIP->getSequence(), incomingXGIP->getCommand()); - xboxOneAuthData->xboneState = XboxOneState::send_auth_console_to_dongle; + xboxOneAuthData->xboneState = GPAuthState::send_auth_console_to_dongle; incomingXGIP->reset(); } } @@ -623,7 +623,7 @@ void XBOneDriver::update() { break; case SETUP_AUTH: // Received packet from dongle to console / PC - if ( xboxOneAuthData->xboneState == XboxOneState::send_auth_dongle_to_console ) { + if ( xboxOneAuthData->xboneState == GPAuthState::send_auth_dongle_to_console ) { uint16_t len = xboxOneAuthData->dongleBuffer.length; uint8_t type = xboxOneAuthData->dongleBuffer.type; uint8_t sequence = xboxOneAuthData->dongleBuffer.sequence; @@ -637,10 +637,10 @@ void XBOneDriver::update() { } // Process auth dongle to console - if ( xboxOneAuthData->xboneState == XboxOneState::wait_auth_dongle_to_console ) { + if ( xboxOneAuthData->xboneState == GPAuthState::wait_auth_dongle_to_console ) { queue_xbone_report(outgoingXGIP->generatePacket(), outgoingXGIP->getPacketLength()); if ( outgoingXGIP->getChunked() == false || outgoingXGIP->endOfChunk() == true ) { - xboxOneAuthData->xboneState = XboxOneState::auth_idle_state; + xboxOneAuthData->xboneState = GPAuthState::auth_idle_state; } if ( outgoingXGIP->getPacketAck() == 1 ) { // ACK can happen at different chunks set_ack_wait(); diff --git a/src/drivers/xinput/XInputAuthUSBListener.cpp b/src/drivers/xinput/XInputAuthUSBListener.cpp index 799b0eeea..ceb62467a 100644 --- a/src/drivers/xinput/XInputAuthUSBListener.cpp +++ b/src/drivers/xinput/XInputAuthUSBListener.cpp @@ -19,7 +19,7 @@ void XInputAuthUSBListener::setAuthData(XInputAuthData * authData ) { xinputAuthData->dongle_ready = false; memset(xinputAuthData->dongleSerial, 0, X360_AUTHLEN_DONGLE_SERIAL); xinputAuthData->passthruBufferLen = 0; - xinputAuthData->xinputState = XInputAuthState::auth_idle_state; + xinputAuthData->xinputState = GPAuthState::auth_idle_state; } void XInputAuthUSBListener::setup() { @@ -93,7 +93,7 @@ void XInputAuthUSBListener::process() { // Idle State, check for incoming console data if ( dongleAuthState == DONGLE_AUTH_STATE::DONGLE_AUTH_IDLE ) { // Received a packet from the console to dongle - if ( xinputAuthData->xinputState == XInputAuthState::send_auth_console_to_dongle ) { + if ( xinputAuthData->xinputState == GPAuthState::send_auth_console_to_dongle ) { switch(xinputAuthData->passthruBufferID) { case XSM360AuthRequest::XSM360_INIT_AUTH: // Copy to our initial auth buffer incase the dongle reconnects @@ -104,7 +104,7 @@ void XInputAuthUSBListener::process() { // Actions are performed in this order if ( auth_dongle_init_challenge() == false) { - xinputAuthData->xinputState = XInputAuthState::auth_idle_state; + xinputAuthData->xinputState = GPAuthState::auth_idle_state; return; } auth_dongle_wait(XSM360AuthRequest::XSM360_INIT_AUTH); @@ -112,7 +112,7 @@ void XInputAuthUSBListener::process() { case XSM360AuthRequest::XSM360_VERIFY_AUTH: // Challenge Verify (22 bytes) if ( auth_dongle_challenge_verify() == false) { - xinputAuthData->xinputState = XInputAuthState::auth_idle_state; + xinputAuthData->xinputState = GPAuthState::auth_idle_state; return; } auth_dongle_wait(XSM360AuthRequest::XSM360_VERIFY_AUTH); @@ -132,17 +132,17 @@ void XInputAuthUSBListener::process() { case XSM360AuthRequest::XSM360_INIT_AUTH: // Actions are performed in this order if ( auth_dongle_data_reply(X360_AUTHLEN_DONGLE_INIT) == false ) { - xinputAuthData->xinputState = XInputAuthState::auth_idle_state; + xinputAuthData->xinputState = GPAuthState::auth_idle_state; } else { auth_dongle_keepalive(); - xinputAuthData->xinputState = XInputAuthState::send_auth_dongle_to_console; + xinputAuthData->xinputState = GPAuthState::send_auth_dongle_to_console; } break; case XSM360AuthRequest::XSM360_VERIFY_AUTH: if ( auth_dongle_data_reply(X360_AUTHLEN_CHALLENGE) == false ) { - xinputAuthData->xinputState = XInputAuthState::auth_idle_state; + xinputAuthData->xinputState = GPAuthState::auth_idle_state; } else { - xinputAuthData->xinputState = XInputAuthState::send_auth_dongle_to_console; + xinputAuthData->xinputState = GPAuthState::send_auth_dongle_to_console; } break; default: @@ -156,7 +156,7 @@ void XInputAuthUSBListener::process() { dongleAuthState = DONGLE_AUTH_STATE::DONGLE_AUTH_IDLE; wait_count = 0; wait_time = 0; - xinputAuthData->xinputState = XInputAuthState::auth_idle_state; + xinputAuthData->xinputState = GPAuthState::auth_idle_state; } } } diff --git a/src/drivers/xinput/XInputDriver.cpp b/src/drivers/xinput/XInputDriver.cpp index fe466a9f6..62fe1b972 100644 --- a/src/drivers/xinput/XInputDriver.cpp +++ b/src/drivers/xinput/XInputDriver.cpp @@ -352,7 +352,7 @@ bool XInputDriver::vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_co memcpy(tud_buffer, xinputAuthData->dongleSerial, len); break; case XSM360_RESPOND_CHALLENGE: - if ( xinputAuthData->xinputState == XInputAuthState::send_auth_dongle_to_console ) { + if ( xinputAuthData->xinputState == GPAuthState::send_auth_dongle_to_console ) { memcpy(tud_buffer, xinputAuthData->passthruBuffer, xinputAuthData->passthruBufferLen); len = xinputAuthData->passthruBufferLen; } else { @@ -365,7 +365,7 @@ bool XInputDriver::vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_co break; case XSM360_REQUEST_STATE: // State Ready = 2, Not-Ready = 1 - if ( xinputAuthData->xinputState == XInputAuthState::send_auth_dongle_to_console ) { + if ( xinputAuthData->xinputState == GPAuthState::send_auth_dongle_to_console ) { state = 2; } else { state = 1; @@ -385,18 +385,18 @@ bool XInputDriver::vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_co // Buf is filled, we can save the data to our auth switch (request->bRequest) { case XSM360AuthRequest::XSM360_INIT_AUTH: - if ( xinputAuthData->xinputState == XInputAuthState::auth_idle_state ) { + if ( xinputAuthData->xinputState == GPAuthState::auth_idle_state ) { memcpy(xinputAuthData->passthruBuffer, tud_buffer, request->wLength); xinputAuthData->passthruBufferLen = request->wLength; xinputAuthData->passthruBufferID = XSM360AuthRequest::XSM360_INIT_AUTH; - xinputAuthData->xinputState = XInputAuthState::send_auth_console_to_dongle; + xinputAuthData->xinputState = GPAuthState::send_auth_console_to_dongle; } break; case XSM360AuthRequest::XSM360_VERIFY_AUTH: memcpy(xinputAuthData->passthruBuffer, tud_buffer, request->wLength); xinputAuthData->passthruBufferLen = request->wLength; xinputAuthData->passthruBufferID = XSM360AuthRequest::XSM360_VERIFY_AUTH; - xinputAuthData->xinputState = XInputAuthState::send_auth_console_to_dongle; + xinputAuthData->xinputState = GPAuthState::send_auth_console_to_dongle; break; default: break; diff --git a/src/main.cpp b/src/main.cpp index 66c694060..f87ccdec3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -45,7 +45,7 @@ int main() { // Sync Core0 and Core1 while(gp2040Core1->ready() == false ) { - __asm volatile ("nop\n"); + tud_task(); } gp2040Core0->run();