diff --git a/include/jbus/Endpoint.hpp b/include/jbus/Endpoint.hpp index cc29a42..2abd348 100644 --- a/include/jbus/Endpoint.hpp +++ b/include/jbus/Endpoint.hpp @@ -40,43 +40,12 @@ class Endpoint u32 xc_progLength; /* Unwrapped public key */ - u32 x20_publicKey; + u32 x20_key; /* Message authentication code */ u32 x24_authInitCode; - void ProcessGBACrypto() - { - /* Unwrap key from challenge using 'sedo' magic number (to encrypt JoyBoot program) */ - x20_publicKey = x0_gbaChallenge ^ 0x6f646573; - - /* Pack palette parameters */ - u16 paletteSpeedCoded; - s16 logoSpeed = static_cast(x8_logoSpeed); - if (logoSpeed < 0) - paletteSpeedCoded = ((-logoSpeed + 2) * 2) | (x4_logoPalette << 4); - else if (logoSpeed == 0) - paletteSpeedCoded = (x4_logoPalette * 2) | 0x70; - else /* logo_speed > 0 */ - paletteSpeedCoded = ((logoSpeed - 1) * 2) | (x4_logoPalette << 4); - - /* JoyBoot ROMs start with a padded header; this is the length beyond that header */ - s32 lengthNoHeader = ROUND_UP_8(xc_progLength) - 0x200; - - /* The JoyBus protocol transmits in 4-byte packets while flipping a state flag; - * so the GBA BIOS counts the program length in 8-byte packet-pairs */ - u16 packetPairCount = (lengthNoHeader < 0) ? 0 : lengthNoHeader / 8; - paletteSpeedCoded |= (packetPairCount & 0x4000) >> 14; - - /* Pack together encoded transmission parameters */ - u32 t1 = (((packetPairCount << 16) | 0x3f80) & 0x3f80ffff) * 2; - t1 += (static_cast(static_cast(t1 >> 8)) & packetPairCount) << 16; - u32 t2 = ((paletteSpeedCoded & 0xff) << 16) + (t1 & 0xff0000) + ((t1 >> 8) & 0xffff00); - u32 t3 = paletteSpeedCoded << 16 | ((t2 << 8) & 0xff000000) | (t1 >> 16) | 0x80808080; - - /* Wrap with 'Kawa' or 'sedo' (Kawasedo is the author of the BIOS cipher) */ - x24_authInitCode = t3 ^ ((t3 & 0x200) != 0 ? 0x6f646573 : 0x6177614b); - } + void ProcessGBACrypto(); } xf8_dspHmac; s32 x0_pColor; diff --git a/include/jbus/Socket.hpp b/include/jbus/Socket.hpp index 6bfe237..ff4070d 100644 --- a/include/jbus/Socket.hpp +++ b/include/jbus/Socket.hpp @@ -134,7 +134,7 @@ class Socket m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (m_socket == -1) { - fprintf(stderr, "Can't allocate socket\n"); + fprintf(stderr, "jbus: Can't allocate socket\n"); return false; } @@ -223,14 +223,14 @@ public: if (bind(m_socket, reinterpret_cast(&addr), sizeof(addr)) == -1) { /* Not likely to happen, but... */ - fprintf(stderr, "Failed to bind listener socket to port %d\n", port); + fprintf(stderr, "jbus: Failed to bind listener socket to port %d\n", port); return false; } if (::listen(m_socket, 0) == -1) { /* Oops, socket is deaf */ - fprintf(stderr, "Failed to listen to port %d\n", port); + fprintf(stderr, "jbus: Failed to listen to port %d\n", port); return false; } @@ -252,11 +252,11 @@ public: #ifndef _WIN32 EResult res = (errno == EAGAIN) ? EResult::Busy : EResult::Error; if (res == EResult::Error) - fprintf(stderr, "Failed to accept incoming connection: %s\n", strerror(errno)); + fprintf(stderr, "jbus: Failed to accept incoming connection: %s\n", strerror(errno)); #else EResult res = LastWSAError(); if (res == EResult::Error) - fprintf(stderr, "Failed to accept incoming connection\n"); + fprintf(stderr, "jbus: Failed to accept incoming connection\n"); #endif return res; } diff --git a/lib/Endpoint.cpp b/lib/Endpoint.cpp index 647502a..cb3d2c9 100644 --- a/lib/Endpoint.cpp +++ b/lib/Endpoint.cpp @@ -5,6 +5,39 @@ namespace jbus { +void Endpoint::KawasedoChallenge::DSPSecParms::ProcessGBACrypto() +{ + /* Unwrap key from challenge using 'sedo' magic number (to encrypt JoyBoot program) */ + x20_key = x0_gbaChallenge ^ 0x6f646573; + + /* Pack palette parameters */ + u16 paletteSpeedCoded; + s16 logoSpeed = static_cast(x8_logoSpeed); + if (logoSpeed < 0) + paletteSpeedCoded = ((-logoSpeed + 2) * 2) | (x4_logoPalette << 4); + else if (logoSpeed == 0) + paletteSpeedCoded = (x4_logoPalette * 2) | 0x70; + else /* logo_speed > 0 */ + paletteSpeedCoded = ((logoSpeed - 1) * 2) | (x4_logoPalette << 4); + + /* JoyBoot ROMs start with a padded header; this is the length beyond that header */ + s32 lengthNoHeader = ROUND_UP_8(xc_progLength) - 0x200; + + /* The JoyBus protocol transmits in 4-byte packets while flipping a state flag; + * so the GBA BIOS counts the program length in 8-byte packet-pairs */ + u16 packetPairCount = (lengthNoHeader < 0) ? 0 : lengthNoHeader / 8; + paletteSpeedCoded |= (packetPairCount & 0x4000) >> 14; + + /* Pack together encoded transmission parameters */ + u32 t1 = (((packetPairCount << 16) | 0x3f80) & 0x3f80ffff) * 2; + t1 += (static_cast(static_cast(t1 >> 8)) & packetPairCount) << 16; + u32 t2 = ((paletteSpeedCoded & 0xff) << 16) + (t1 & 0xff0000) + ((t1 >> 8) & 0xffff00); + u32 t3 = paletteSpeedCoded << 16 | ((t2 << 8) & 0xff000000) | (t1 >> 16) | 0x80808080; + + /* Wrap with 'Kawa' or 'sedo' (Kawasedo is the author of the BIOS cipher) */ + x24_authInitCode = t3 ^ ((t3 & 0x200) != 0 ? 0x6f646573 : 0x6177614b); +} + void Endpoint::KawasedoChallenge::_0Reset(ThreadLocalEndpoint& endpoint, EJoyReturn status) { if (status != GBA_READY || @@ -87,7 +120,7 @@ void Endpoint::KawasedoChallenge::_DSPCryptoInit() void Endpoint::KawasedoChallenge::_DSPCryptoDone(ThreadLocalEndpoint& endpoint) { - x58_currentKey = xf8_dspHmac.x20_publicKey; + x58_currentKey = xf8_dspHmac.x20_key; x5c_initMessage = xf8_dspHmac.x24_authInitCode; x20_byteInWindow = ROUND_UP_8(xc_progLen); diff --git a/lib/Listener.cpp b/lib/Listener.cpp index b8be591..4b37014 100644 --- a/lib/Listener.cpp +++ b/lib/Listener.cpp @@ -12,7 +12,7 @@ void Listener::listenerProc() printf("JoyBus listener started\n"); #endif - net::IPAddress localhost("0.0.0.0"); + net::IPAddress localhost("127.0.0.1"); bool dataBound = false; bool clockBound = false; while (m_running && (!dataBound || !clockBound))